簡體   English   中英

如何判斷哪個控件訪問了具有多個引用的方法?

[英]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 = sender as 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM