[英]How to tell which control accessed a method with multiple references?
I have 25 buttons that I've assigned into 1 method (snippet below). 我已经将25个按钮分配给1种方法(下面的代码段)。 I want to be able to grab the clicked button's informations such as tags and name, and set the
.Enabled
property to false. 我希望能够获取单击的按钮的信息(例如标签和名称),并将
.Enabled
属性设置为false。 I have this following code; 我有以下代码;
int picksLeft = 5;
int value = 100;
string multiplier;
string buttonName;
private void btnPick_Click(object sender, EventArgs e) //My method with 25 references (buttons)
{
for (int i = 5; i > 0; i--)
{
if (lblPicks.Text == "Picks Left: " + i)
{
picksLeft = i - 1;
}
}
lblPicks.Text = "Picks Left: " + picksLeft.ToString();
//Get tag string, disable the button.
//multiplier = buttonName.Tag;
//Controls[buttonName].Enabled = false;
//value -= value * Convert.ToDouble(multiplier);
}
The sender object contains all the information, just cast it to button. 发送者对象包含所有信息,只需将其强制转换为按钮即可。 As I remember it should look like this: button = sender as button;
我记得它应该像这样:button = sender as button; Then use it to refer to any of its properties.
然后使用它来引用其任何属性。
I hope it would help. 希望对您有所帮助。
Button btn;
private void btnPick_Click(object sender, EventArgs e) //My method with 25 references (buttons)
{
for (int i = 5; i > 0; i--)
{
if (lblPicks.Text == "Picks Left: " + i)
{
picksLeft = i - 1;
}
}
lblPicks.Text = "Picks Left: " + picksLeft.ToString();
btn = sender as Button;
btn.Enabled = false;
multiplier = btn.Tag;
value -= value * Convert.ToDouble(multiplier);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.