📄 sellcar.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
namespace Car_Inventory_Control
{
/// <summary>
/// Summary description for Sellcar.
/// </summary>
public class frmSellcar : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblCarPurchaseRate;
private System.Windows.Forms.Label lblCarModelNumber;
private System.Windows.Forms.ComboBox cboCarModelNo;
private System.Windows.Forms.Label lblCarSale;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label lblCarPurchaseRateDetail;
private System.Windows.Forms.Button btnExit;
private FileStream fstream;
private BinaryReader breader ;
private BinaryWriter bwriter;
private Hashtable objCarDetails,objCarSaleDetails;
double purchaseRate;
double saleRate;
private System.Windows.Forms.Button btnSell;
private System.Windows.Forms.TextBox txtCarSaleRate;
string modelNum;
string filename = "d:\\CarSaleRecord.txt";
public frmSellcar()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblCarPurchaseRate = new System.Windows.Forms.Label();
this.lblCarModelNumber = new System.Windows.Forms.Label();
this.cboCarModelNo = new System.Windows.Forms.ComboBox();
this.txtCarSaleRate = new System.Windows.Forms.TextBox();
this.lblCarSale = new System.Windows.Forms.Label();
this.lblCarPurchaseRateDetail = new System.Windows.Forms.Label();
this.btnExit = new System.Windows.Forms.Button();
this.btnSell = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblCarPurchaseRate
//
this.lblCarPurchaseRate.Location = new System.Drawing.Point(38, 60);
this.lblCarPurchaseRate.Name = "lblCarPurchaseRate";
this.lblCarPurchaseRate.Size = new System.Drawing.Size(125, 25);
this.lblCarPurchaseRate.TabIndex = 3;
this.lblCarPurchaseRate.Text = "汽车购买价格:";
//
// lblCarModelNumber
//
this.lblCarModelNumber.Location = new System.Drawing.Point(38, 17);
this.lblCarModelNumber.Name = "lblCarModelNumber";
this.lblCarModelNumber.Size = new System.Drawing.Size(125, 25);
this.lblCarModelNumber.TabIndex = 2;
this.lblCarModelNumber.Text = "汽车型号:";
//
// cboCarModelNo
//
this.cboCarModelNo.Location = new System.Drawing.Point(202, 17);
this.cboCarModelNo.Name = "cboCarModelNo";
this.cboCarModelNo.Size = new System.Drawing.Size(144, 20);
this.cboCarModelNo.TabIndex = 4;
this.cboCarModelNo.SelectedIndexChanged += new System.EventHandler(this.cboCarModelNo_SelectedIndexChanged);
//
// txtCarSaleRate
//
this.txtCarSaleRate.Location = new System.Drawing.Point(202, 112);
this.txtCarSaleRate.Name = "txtCarSaleRate";
this.txtCarSaleRate.Size = new System.Drawing.Size(144, 21);
this.txtCarSaleRate.TabIndex = 7;
this.txtCarSaleRate.Text = "";
//
// lblCarSale
//
this.lblCarSale.Location = new System.Drawing.Point(38, 112);
this.lblCarSale.Name = "lblCarSale";
this.lblCarSale.Size = new System.Drawing.Size(125, 25);
this.lblCarSale.TabIndex = 6;
this.lblCarSale.Text = "汽车销售价格:";
//
// lblCarPurchaseRateDetail
//
this.lblCarPurchaseRateDetail.Location = new System.Drawing.Point(202, 60);
this.lblCarPurchaseRateDetail.Name = "lblCarPurchaseRateDetail";
this.lblCarPurchaseRateDetail.Size = new System.Drawing.Size(144, 25);
this.lblCarPurchaseRateDetail.TabIndex = 8;
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(259, 164);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(90, 24);
this.btnExit.TabIndex = 10;
this.btnExit.Text = "退出(&E)";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// btnSell
//
this.btnSell.Location = new System.Drawing.Point(144, 164);
this.btnSell.Name = "btnSell";
this.btnSell.Size = new System.Drawing.Size(90, 24);
this.btnSell.TabIndex = 9;
this.btnSell.Text = "销售(&S)";
this.btnSell.Click += new System.EventHandler(this.btnSell_Click);
//
// frmSellcar
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(355, 194);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnSell);
this.Controls.Add(this.lblCarPurchaseRateDetail);
this.Controls.Add(this.txtCarSaleRate);
this.Controls.Add(this.lblCarSale);
this.Controls.Add(this.cboCarModelNo);
this.Controls.Add(this.lblCarPurchaseRate);
this.Controls.Add(this.lblCarModelNumber);
this.Name = "frmSellcar";
this.ShowInTaskbar = false;
this.Text = "销售汽车";
this.Load += new System.EventHandler(this.Sellcar_Load);
this.ResumeLayout(false);
}
#endregion
private void Sellcar_Load(object sender, System.EventArgs e)
{
string filename = "d:\\CarMasterRecord.txt";
objCarDetails = new Hashtable();
try
{
fstream = File.Open(filename,FileMode.Open,FileAccess.Read);
}
catch(FileNotFoundException ex)
{
MessageBox.Show(ex.Message.ToString());
}
breader = new BinaryReader(fstream);
//read masterrecord file and load ComboBox with model numbers
try
{
while(true)
{
modelNum = breader.ReadString();
purchaseRate = breader.ReadDouble();
objCarDetails.Add(modelNum,purchaseRate);
}
}
catch(EndOfStreamException ex)
{
MessageBox.Show("到达流末尾");
}
foreach(DictionaryEntry car in objCarDetails)
{
this.cboCarModelNo.Items.Add(car.Key);
}
fstream.Close();
filename = "D:\\CarSaleRecord.txt";
try
{
fstream = File.Open(filename,FileMode.OpenOrCreate,FileAccess.ReadWrite);
}
catch(FileNotFoundException ex)
{
MessageBox.Show(ex.Message.ToString());
}
bwriter = new BinaryWriter(fstream);
}
//display purchase rate depends on model number selected
private void cboCarModelNo_SelectedIndexChanged(object sender, System.EventArgs e)
{
foreach(DictionaryEntry car in objCarDetails)
{
if(string.Compare(car.Key.ToString(),cboCarModelNo.SelectedItem.ToString(),false)==0)
this.lblCarPurchaseRateDetail.Text = car.Value.ToString();
}
}
private void btnExit_Click(object sender, System.EventArgs e)
{
breader.Close();
fstream.Close();
this.Close();
}
//Add the details of sold car into file CarSaleRecord.txt
private void btnSell_Click(object sender, System.EventArgs e)
{
for(int i=0; i<this.txtCarSaleRate.Text.Length; i++)
{
if(!Char.IsNumber(txtCarSaleRate.Text.ToString(),i))
{
MessageBox.Show("价格应为数字");
return;
break;
}
}
if(this.txtCarSaleRate.Text != string.Empty)
{
purchaseRate = Double.Parse(this.lblCarPurchaseRateDetail.Text);
saleRate = Double.Parse(this.txtCarSaleRate.Text);
bwriter.Write(this.cboCarModelNo.SelectedItem.ToString());
bwriter.Write(purchaseRate);
bwriter.Write(saleRate);
}
else
MessageBox.Show("提供完整的数据");
this.txtCarSaleRate.Clear();
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -