回复 9楼 yhlvht
多谢!
程序代码:
namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.components = new (); this.serialPort1 = new (this.components); this.button1 = new ButtonEx(); this.button2 = new ButtonEx(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(69, 52); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; // // button2 // this.button2.Location = new System.Drawing.Point(69, 103); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 1; this.button2.Text = "button2"; this.button2.UseVisualStyleBackColor = true; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(284, 262); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion private serialPort1; private ButtonEx button1; private ButtonEx button2; } }
程序代码:
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.button1.Click += new EventHandler(button1_Click); this.button2.Click += new EventHandler(button1_Click); } void button1_Click(object sender, EventArgs e) { Button b = sender as Button; EventArgsEx ex = (EventArgsEx)e; if (ex.I == 1) { MessageBox.Show("111111"); } else if (ex.I == 2) { MessageBox.Show("222222"); } } } /// <summary> /// 继承EventArgs 加一个属性 /// </summary> public class EventArgsEx : EventArgs { public int I { get; set; } } /// <summary> /// 继承Button 重写OnClick方法 /// 传入被修改过的EventArgsEx参数 /// </summary> public class ButtonEx : Button { protected override void OnClick(EventArgs e) { EventArgsEx ex = new EventArgsEx(); if (this.Name == "button1") { ex.I = 1; } else if (this.Name == "button2") { ex.I = 2; } base.OnClick(ex); } } }