如何判断哪个控件访问了多个引用的方法?

时间:2016-06-19 03:29:59

标签: c# winforms identifier

我有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);
    }

1 个答案:

答案 0 :(得分:0)

发件人对象包含所有信息,只需将其转换为按钮即可。 我记得它应该是这样的: button =发送者为按钮; 然后用它来引用它的任何属性。

我希望它会有所帮助。

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);
}