如何为动态按钮数组创建点击事件 [英] how to create click events for dynamic button array
本文介绍了如何为动态按钮数组创建点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为动态按钮阵列创建按钮单击事件?
请帮助我.
how can i create a button click event for dynamic button array?
please help me.
推荐答案
在这里,看看: ^ ]
Here, have a look: Creating Control Arrays in Visual Basic .NET and Visual C# .NET[^]
在代码中创建按钮时,添加描述事件处理程序,例如:
When you create the button in your code add describe the event handler, something like:
Button btn = new Button();
...
btn.Click += new EventHandler(commonEventHandlerMethodForAllButtons);
如果愿意,可以使用Tag
属性添加有关按钮的信息,您可以在处理事件的方法中使用该按钮.例如:
If you like, you can use Tag
property to add information about the button which you can use in the method handling the event. For an example:
enum Action {
Some = 1,
Other = 2
}
...
void SomeMethod() {
Button btn = new Button();
btn.Tag = Action.Some;
btn.Click += new EventHandler(common_Click);
...
}
void common_Click(object sender, EventArgs e) {
switch (((Action)(Button)sender).Tag) {
case Action.Some:
// do something
break;
case ...
}
}
这篇关于如何为动态按钮数组创建点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文