Kev*_*vin 5 c# user-interface components winforms
是否可以创建一系列控件?如果数组中的多个控件共享同一个事件处理程序,有没有办法获取控件的索引?
这当然是可行的.在这种情况下,共享事件处理程序相当容易,因为Button
引发事件的事件是作为事件args的一部分发送的.这将是sender
价值,可以回归到Button
这是一些示例代码
class Form1 : Form {
private Button[] _buttons;
public Form1(int count) {
_buttons = new Button[count];
for ( int i = 0; i < count; i++ ) {
var b = new Button();
b.Text = "Button" + i.ToString()
b.Click += new EventHandler(OnButtonClick);
_buttons[i] = b;
}
}
private void OnButtonClick(object sender, EventArgs e) {
var whichButton = (Button)sender;
...
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11484 次 |
最近记录: |