[英]Handle event by list of windows form controls
I have one Windows Form within a lot of ListBoxes
, and every ListBox
have to do the same thing. 我在很多
ListBoxes
都有一个Windows窗体,每个ListBox
都必须做相同的事情。 I think to handle these like a list. 我认为像列表一样处理这些问题。
OnLoad
I create the list: OnLoad
我创建列表:
private List<ListBox> lsts = new List<ListBox>();
lsts.Add(lstStart);
lsts.Add(lst0);
lsts.Add(lst1);
lsts.Add(lst2);
lsts.Add(lst3);
How can I write the SelectedIndexChanged
Method for all ListBoxes
in my List
? 我怎么能写
SelectedIndexChanged
方法全部ListBoxes
在我的List
?
Try for answer, I didn't found a tutorial for that. 尝试回答,但我没有找到相关的教程。
Use this code first to generate SelectedIndexChanged
for all ListBoxes
: 首先使用此代码为所有
ListBoxes
生成SelectedIndexChanged
:
lsts.ForEach(c => c.SelectedIndexChanged += lsts_SelectedIndexChanged);
And: 和:
private void lsts_SelectedIndexChanged(object sender, EventArgs e)
{
//Use sender to find the selected ListBox
var selectedListBox = (ListBox)sender;
//Do what you want with selected ListBox
MessageBox.Show(selectedListBox.Name);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.