[英]Access control properties from its dynamically created Event Handler
I am developing a C# WinForms
application. 我正在开发C#
WinForms
应用程序。 On the FormLoad
event, I dynamically create and add to the form 100 buttons without text, but with names like button1
, button2
etc. Also, onto these buttons, after their creation, I dynamically link a unique event handler for the ButtonClick
event. 在
FormLoad
事件上,我动态创建并添加到窗体100中的按钮,这些按钮没有文本,但是具有诸如button1
, button2
等之类的名称。此外,在这些按钮上,创建它们之后,我为ButtonClick
事件动态链接了唯一的事件处理程序。 How can I access the button's properties from within the event handler (more specifically the button name)? 如何从事件处理程序(更具体地说是按钮名称)中访问按钮的属性? If I use this.Name, I get the name of the form, and not the name of the button.
如果使用this.Name,我将得到表单的名称,而不是按钮的名称。
Later Edit: (for those who might wonder here in search of solutions) 以后编辑:(对于那些可能在这里寻找解决方案的人)
private void function1()
{
Button a = new Button();
a.Name = "button" + (i * j).ToString();
a.Click += new EventHandler(OnFieldButtonClicked);
}
private void OnFieldButtonClicked(object sender, EventArgs e)
{
Button button = (Button)sender;
MessageBox.Show(button.Name);
}
The sender
argument is the event handler encapsulates an instance of the object that has triggered the event: sender
参数是事件处理程序,它封装了触发事件的对象的实例:
Button button = (Button) sender;
String text = button.Text;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.