📄 frmpurchaseitem.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Example_3
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmPurchaseItem : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblProductCode;
private System.Windows.Forms.Label lblProductName;
private System.Windows.Forms.Label lblProductRate;
private System.Windows.Forms.Label lblUnits;
private System.Windows.Forms.TextBox txtProductCode;
private System.Windows.Forms.TextBox txtProductName;
private System.Windows.Forms.TextBox txtUnits;
private System.Windows.Forms.TextBox txtProductRate;
private System.Windows.Forms.Label lblTotalPrice;
private System.Windows.Forms.TextBox txtTotalPrice;
private System.Windows.Forms.Button btnCalculate;
private System.Windows.Forms.Button btnExit;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmPurchaseItem()
{
//
// 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.lblProductCode = new System.Windows.Forms.Label();
this.lblProductName = new System.Windows.Forms.Label();
this.lblProductRate = new System.Windows.Forms.Label();
this.lblUnits = new System.Windows.Forms.Label();
this.txtProductCode = new System.Windows.Forms.TextBox();
this.txtProductName = new System.Windows.Forms.TextBox();
this.txtUnits = new System.Windows.Forms.TextBox();
this.txtProductRate = new System.Windows.Forms.TextBox();
this.lblTotalPrice = new System.Windows.Forms.Label();
this.txtTotalPrice = new System.Windows.Forms.TextBox();
this.btnCalculate = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblProductCode
//
this.lblProductCode.Location = new System.Drawing.Point(8, 16);
this.lblProductCode.Name = "lblProductCode";
this.lblProductCode.Size = new System.Drawing.Size(80, 23);
this.lblProductCode.TabIndex = 0;
this.lblProductCode.Text = "产品代码:";
//
// lblProductName
//
this.lblProductName.Location = new System.Drawing.Point(8, 48);
this.lblProductName.Name = "lblProductName";
this.lblProductName.Size = new System.Drawing.Size(80, 23);
this.lblProductName.TabIndex = 1;
this.lblProductName.Text = "产品名称:";
//
// lblProductRate
//
this.lblProductRate.Location = new System.Drawing.Point(8, 80);
this.lblProductRate.Name = "lblProductRate";
this.lblProductRate.Size = new System.Drawing.Size(64, 23);
this.lblProductRate.TabIndex = 2;
this.lblProductRate.Text = "价格:";
//
// lblUnits
//
this.lblUnits.Location = new System.Drawing.Point(8, 112);
this.lblUnits.Name = "lblUnits";
this.lblUnits.Size = new System.Drawing.Size(64, 23);
this.lblUnits.TabIndex = 3;
this.lblUnits.Text = "单位:";
//
// txtProductCode
//
this.txtProductCode.Location = new System.Drawing.Point(88, 16);
this.txtProductCode.Name = "txtProductCode";
this.txtProductCode.TabIndex = 4;
this.txtProductCode.Text = "";
this.txtProductCode.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtProductCode_KeyPress);
//
// txtProductName
//
this.txtProductName.Location = new System.Drawing.Point(88, 48);
this.txtProductName.Name = "txtProductName";
this.txtProductName.TabIndex = 5;
this.txtProductName.Text = "";
//
// txtUnits
//
this.txtUnits.Location = new System.Drawing.Point(88, 112);
this.txtUnits.Name = "txtUnits";
this.txtUnits.TabIndex = 6;
this.txtUnits.Text = "";
//
// txtProductRate
//
this.txtProductRate.Location = new System.Drawing.Point(88, 80);
this.txtProductRate.Name = "txtProductRate";
this.txtProductRate.TabIndex = 7;
this.txtProductRate.Text = "";
//
// lblTotalPrice
//
this.lblTotalPrice.Location = new System.Drawing.Point(8, 144);
this.lblTotalPrice.Name = "lblTotalPrice";
this.lblTotalPrice.Size = new System.Drawing.Size(64, 23);
this.lblTotalPrice.TabIndex = 3;
this.lblTotalPrice.Text = "总价:";
//
// txtTotalPrice
//
this.txtTotalPrice.Enabled = false;
this.txtTotalPrice.Location = new System.Drawing.Point(88, 144);
this.txtTotalPrice.Name = "txtTotalPrice";
this.txtTotalPrice.TabIndex = 6;
this.txtTotalPrice.Text = "";
//
// btnCalculate
//
this.btnCalculate.Location = new System.Drawing.Point(32, 184);
this.btnCalculate.Name = "btnCalculate";
this.btnCalculate.TabIndex = 8;
this.btnCalculate.Text = "计算 (&C)";
this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(112, 184);
this.btnExit.Name = "btnExit";
this.btnExit.TabIndex = 9;
this.btnExit.Text = "退出 (&X)";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// frmPurchaseItem
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(216, 213);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnCalculate);
this.Controls.Add(this.txtProductRate);
this.Controls.Add(this.txtUnits);
this.Controls.Add(this.txtProductName);
this.Controls.Add(this.txtProductCode);
this.Controls.Add(this.lblUnits);
this.Controls.Add(this.lblProductRate);
this.Controls.Add(this.lblProductName);
this.Controls.Add(this.lblProductCode);
this.Controls.Add(this.lblTotalPrice);
this.Controls.Add(this.txtTotalPrice);
this.Font = new System.Drawing.Font("Times New Roman", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Name = "frmPurchaseItem";
this.Text = "购买商品";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmPurchaseItem());
}
private void btnCalculate_Click(object sender, System.EventArgs e)
{
try
{
if(this.txtProductRate.Text.Trim() == string.Empty || this.txtUnits.Text.Trim() == string.Empty)
throw new ExceptionClass("缺少字段值");
else
this.txtTotalPrice.Text = Convert.ToString(Convert.ToDouble(this.txtProductRate.Text) * Convert.ToDouble(this.txtUnits.Text));
}
catch(ExceptionClass ex)
{
MessageBox.Show(ex.Message);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void txtProductCode_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
try
{
//只接受 0-9 和退格键
e.Handled = e.KeyChar < '0' || e.KeyChar > '9';
if(e.KeyChar == (char)8)
e.Handled=false;
//引发异常
if(e.Handled == true)
throw new ExceptionClass("此字段中只能输入数字");
}
////捕获此异常
catch(ExceptionClass ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -