?? form1.cs
字號:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace cs_AddRemoveHandler
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtN1;
private System.Windows.Forms.TextBox txtN2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label lblAns;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnDel;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.txtN1 = new System.Windows.Forms.TextBox();
this.txtN2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.lblAns = new System.Windows.Forms.Label();
this.btnAdd = new System.Windows.Forms.Button();
this.btnDel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtN1
//
this.txtN1.Location = new System.Drawing.Point(40, 40);
this.txtN1.Name = "txtN1";
this.txtN1.Size = new System.Drawing.Size(72, 21);
this.txtN1.TabIndex = 0;
this.txtN1.Text = "";
this.txtN1.TextChanged += new System.EventHandler(this.MyButtonClick);
//
// txtN2
//
this.txtN2.Location = new System.Drawing.Point(152, 40);
this.txtN2.Name = "txtN2";
this.txtN2.Size = new System.Drawing.Size(72, 21);
this.txtN2.TabIndex = 1;
this.txtN2.Text = "";
this.txtN2.TextChanged += new System.EventHandler(this.MyButtonClick);
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋體", 18F);
this.label1.Location = new System.Drawing.Point(120, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(24, 23);
this.label1.TabIndex = 2;
this.label1.Text = "+";
//
// label2
//
this.label2.Font = new System.Drawing.Font("宋體", 18F);
this.label2.Location = new System.Drawing.Point(224, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(24, 23);
this.label2.TabIndex = 3;
this.label2.Text = "=";
//
// lblAns
//
this.lblAns.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(255)), ((System.Byte)(192)));
this.lblAns.Font = new System.Drawing.Font("宋體", 12F);
this.lblAns.Location = new System.Drawing.Point(264, 40);
this.lblAns.Name = "lblAns";
this.lblAns.Size = new System.Drawing.Size(104, 24);
this.lblAns.TabIndex = 4;
//
// btnAdd
//
this.btnAdd.Font = new System.Drawing.Font("宋體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnAdd.Location = new System.Drawing.Point(40, 80);
this.btnAdd.Name = "btnAdd";
this.btnAdd.TabIndex = 5;
this.btnAdd.Text = "新增事件";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnDel
//
this.btnDel.Font = new System.Drawing.Font("宋體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnDel.Location = new System.Drawing.Point(152, 80);
this.btnDel.Name = "btnDel";
this.btnDel.TabIndex = 6;
this.btnDel.Text = "刪除事件";
this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(408, 198);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnDel,
this.btnAdd,
this.lblAns,
this.label2,
this.label1,
this.txtN2,
this.txtN1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void MyButtonClick(object sender, System.EventArgs e)
{
try
{
lblAns.Text =
(float.Parse(txtN1.Text) + float.Parse(txtN2.Text)).ToString();
}
catch
{
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
txtN1.TextChanged += new EventHandler(MyButtonClick);
txtN2.TextChanged += new EventHandler(MyButtonClick);
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
txtN1.TextChanged += new EventHandler(MyButtonClick);
txtN2.TextChanged += new EventHandler(MyButtonClick);
}
private void btnDel_Click(object sender, System.EventArgs e)
{
txtN1.TextChanged -= new EventHandler(MyButtonClick);
txtN2.TextChanged -= new EventHandler(MyButtonClick);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -