[英]How do I add a click event to individual dynamically generated objects?
This code I am writing right now generates a variable amount of independent objects.我现在正在编写的这段代码生成了可变数量的独立对象。 Can I give each of those objects the property that if it it clicked, that single object changes color?
我可以给每个对象一个属性,如果它被点击,单个对象会改变颜色吗?
Here is an example using a button.这是使用按钮的示例。
var button = new Button();
//set other properties...
button.Click += Button_Click; //the event already exists
Now cast the sender (the control that was clicked) and change the back color现在投射发件人(被点击的控件)并更改背景颜色
private void Button_Click(object sender, EventArgs e)
{
var theButton = (Button)sender;
theButton.BackColor = Color.Red;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.