📄 addcar.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 Addcar.
/// </summary>
public class frmAddcar : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblCarModelNumber;
private System.Windows.Forms.Label lblCarPurchaseRate;
private System.Windows.Forms.TextBox txtCarModelNumber;
private System.Windows.Forms.TextBox txtCarPurchaseRate;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnExit;
private FileStream fstream;
private BinaryWriter bwriter;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmAddcar()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
string filename = "D:\\CarMasterRecord.txt";
fstream = File.Open(filename,FileMode.OpenOrCreate,FileAccess.ReadWrite);
//
// 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.lblCarModelNumber = new System.Windows.Forms.Label();
this.lblCarPurchaseRate = new System.Windows.Forms.Label();
this.txtCarModelNumber = new System.Windows.Forms.TextBox();
this.txtCarPurchaseRate = new System.Windows.Forms.TextBox();
this.btnAdd = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblCarModelNumber
//
this.lblCarModelNumber.Location = new System.Drawing.Point(48, 52);
this.lblCarModelNumber.Name = "lblCarModelNumber";
this.lblCarModelNumber.Size = new System.Drawing.Size(125, 24);
this.lblCarModelNumber.TabIndex = 0;
this.lblCarModelNumber.Text = "汽车型号:";
//
// lblCarPurchaseRate
//
this.lblCarPurchaseRate.Location = new System.Drawing.Point(48, 103);
this.lblCarPurchaseRate.Name = "lblCarPurchaseRate";
this.lblCarPurchaseRate.Size = new System.Drawing.Size(125, 25);
this.lblCarPurchaseRate.TabIndex = 1;
this.lblCarPurchaseRate.Text = "汽车购买价格:";
//
// txtCarModelNumber
//
this.txtCarModelNumber.Location = new System.Drawing.Point(221, 46);
this.txtCarModelNumber.Name = "txtCarModelNumber";
this.txtCarModelNumber.Size = new System.Drawing.Size(120, 21);
this.txtCarModelNumber.TabIndex = 2;
this.txtCarModelNumber.Text = "";
//
// txtCarPurchaseRate
//
this.txtCarPurchaseRate.Location = new System.Drawing.Point(221, 103);
this.txtCarPurchaseRate.Name = "txtCarPurchaseRate";
this.txtCarPurchaseRate.Size = new System.Drawing.Size(120, 21);
this.txtCarPurchaseRate.TabIndex = 3;
this.txtCarPurchaseRate.Text = "";
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(134, 172);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(90, 25);
this.btnAdd.TabIndex = 4;
this.btnAdd.Text = "添加(&A)";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(251, 172);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(90, 25);
this.btnExit.TabIndex = 5;
this.btnExit.Text = "退出(&E)";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// frmAddcar
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(384, 212);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.txtCarPurchaseRate);
this.Controls.Add(this.txtCarModelNumber);
this.Controls.Add(this.lblCarPurchaseRate);
this.Controls.Add(this.lblCarModelNumber);
this.Name = "frmAddcar";
this.ShowInTaskbar = false;
this.Text = "添加汽车";
this.Load += new System.EventHandler(this.Addcar_Load);
this.ResumeLayout(false);
}
#endregion
private void btnAdd_Click(object sender, System.EventArgs e)
{
//validates empty fields
//rate must not contain any text
if(this.txtCarModelNumber.Text != string.Empty && this.txtCarPurchaseRate.Text != string.Empty)
{
for(int i=0; i<txtCarPurchaseRate.Text.Length; i++)
{
if(!Char.IsNumber(txtCarPurchaseRate.Text.ToString(),i))
{
MessageBox.Show("价格应为数字");
return;
}
}
//Write model number and purchase rate of car to file CarMasterRecord.txt
string modelnum = this.txtCarModelNumber.Text;
double carrate = Double.Parse(this.txtCarPurchaseRate.Text);
bwriter.Seek(0,SeekOrigin.End);
bwriter.Write(modelnum);
bwriter.Write(carrate);
MessageBox.Show("记录成功添加到文件中");
this.txtCarModelNumber.Clear();
this.txtCarPurchaseRate.Clear();
}
else
MessageBox.Show("提供完整的数据");
}
private void btnExit_Click(object sender, System.EventArgs e)
{
fstream.Close();
fstream.Close();
this.Close();
}
private void Addcar_Load(object sender, System.EventArgs e)
{
bwriter = new BinaryWriter(fstream);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -