this.button1.Click += new System.EventHandler(this.button1_Click); this.button2.Click += new System.EventHandler(this.button1_Click); this.button3.Click += new System.EventHandler(this.button1_Click); this.button4.Click += new System.EventHandler(this.button1_Click); this.button5.Click += new System.EventHandler(this.button1_Click); this.button6.Click += new System.EventHandler(this.button1_Click);
{
Button btn=ctl as Button;
if(btn!=null)
{
btn.Click+=new EventHandler(btnClick);
}
}
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Click += new System.EventHandler(this.button1_Click);
this.button3.Click += new System.EventHandler(this.button1_Click);
this.button4.Click += new System.EventHandler(this.button1_Click);
this.button5.Click += new System.EventHandler(this.button1_Click);
this.button6.Click += new System.EventHandler(this.button1_Click);
----------------------------------------------------
如果里面的方法基本相同怎么办
比如点击这些按钮后
被点击的按钮变为绿色
因为里面要用到this.btn1,this.btn2,
所以每个方法又有点差别
怎么实现集中处理呢?
可不可以把相应的按钮作为参数穿进去?这样实现方法就完全一样了
----------------------------------------------------
{
Button btn = (Button)sender;//获得所点击的按钮
.....//你的操作
}
当然可以作为参数传递,不过你要自己写一个继承于EventArgs的参数类
swich(btn.Text)
{
case "btn1":
{
调用公共方法;
自己的方法;
breake;
}
case "btn2":
{
调用公共方法;
自己的方法;
breake;
}
}按照上面形式,把他放到一个button事件里,然后点击事件都加载这个事件