如何判断哪个控件访问了具有多个引用的方法?
How to tell which control accessed a method with multiple references?
我已将 25 个按钮分配给 1 个方法(下面的代码段)。我希望能够获取点击按钮的标签和名称等信息,并将 .Enabled
属性 设置为 false。我有以下代码;
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);
}
发送者对象包含所有信息,只需将其转换为按钮即可。
我记得它应该是这样的:
按钮 = 发件人作为按钮;
然后用它来指代它的任何属性。
希望对您有所帮助。
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);
}
我已将 25 个按钮分配给 1 个方法(下面的代码段)。我希望能够获取点击按钮的标签和名称等信息,并将 .Enabled
属性 设置为 false。我有以下代码;
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);
}
发送者对象包含所有信息,只需将其转换为按钮即可。 我记得它应该是这样的: 按钮 = 发件人作为按钮; 然后用它来指代它的任何属性。
希望对您有所帮助。
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);
}