如何将事件添加到在运行按钮的列表 [英] How to Add an event to a list of button at runtime
本文介绍了如何将事件添加到在运行按钮的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在运行时创建按钮。我的问题是,我应该如何在运行模式也?
添加事件到每个按钮例如:
按钮BTN;
INT I = 0;
INT J = 0;
清单<按钮和GT; listBTN =新的List<按钮和GT;();私人无效的button1_Click(对象发件人,EventArgs的发送)
{
BTN =新按钮();
btn.Location =新的点(60 + I,90);
btn.Size =新尺寸(50,50);
btn.Name =BTN; listBTN.Add(BTN); I = I + 50; 的foreach(在listBTN键b){
this.Controls.AddRange(新按钮[] {B});
}
}
解决方案
btn.Click + = yourMethod;私人无效yourMethod(对象发件人,EventArgs的发送)
{
//你的实现
按钮BTN =发件人的按钮;
如果(BTN!= NULL)
{
//使用BTN
}
}
如果您希望在声明按钮使用添加事件:
btn.Click + =代表
{
//你的实现
};
I am trying to create buttons at runtime. My Question is that How should I add an Event to each of the buttons at runtime mode also?
For example:
Button btn;
int i =0;
int j =0;
List<Button> listBTN = new List<Button>();
private void button1_Click(object sender, EventArgs e)
{
btn = new Button();
btn.Location = new Point(60 + i, 90);
btn.Size = new Size(50, 50);
btn.Name = "BTN";
listBTN.Add(btn);
i = i + 50;
foreach(Button b in listBTN){
this.Controls.AddRange(new Button[] {b});
}
}
解决方案
btn.Click += yourMethod;
private void yourMethod(object sender, EventArgs e)
{
// your implementation
Button btn = sender as Button;
if (btn != null)
{
//use btn
}
}
if you want to add the event when you declare the button use:
btn.Click += delegate
{
//your implementation
};
这篇关于如何将事件添加到在运行按钮的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文