如何将多个字符串值传递给C#中的Button click事件处理程序?
答案 0 :(得分:13)
如果您事先知道字符串,则可以使用匿名函数捕获并传递字符串
string s1 = ...;
string s2 = ...;
button.Click += (sender, e) => { MyHandler(sender, e, s1, s2); };
void MyHandler(object sender, EventArgs e, string s1, string s2) {
...
}
答案 1 :(得分:2)
在你不知道字符串数量的情况下扩展Jared的答案:
string s1 = ...;
string s2 = ...;
button.Click += (sender, e) => { MyHandler(sender, e, s1, s2); };
button2.Click += (sender, e) => { MyHandler(sender, e, s1, s2, s3, s4); };
void MyHandler(object sender, EventArgs e, params string[] list) {
foreach(string s in list)
{
//do something
}
}