使用相同的方法隐藏单击的按钮WinForm C#

时间:2015-03-23 14:34:28

标签: c# button

我创建了总共27个按钮,其中26个按钮单击了一个。在表单加载时看不到的另一个按钮只有在用户单击其中一个按钮后才能看到,然后单击的按钮将隐藏在表单中。这是我的方法:

private void frmGame_Load(object sender, EventArgs e)
    {
        btn1.Click += clickHandler;
        btn2.Click += clickHandler;
        btn3.Click += clickHandler;
        btn4.Click += clickHandler;
        btn5.Click += clickHandler;
        btn6.Click += clickHandler;
        btn7.Click += clickHandler;
        btn8.Click += clickHandler;
        btn9.Click += clickHandler;
        btn10.Click += clickHandler;
        btn11.Click += clickHandler;
        btn12.Click += clickHandler;
        btn13.Click += clickHandler;
        btn14.Click += clickHandler;
        btn15.Click += clickHandler;
        btn16.Click += clickHandler;
        btn17.Click += clickHandler;
        btn18.Click += clickHandler;
        btn19.Click += clickHandler;
        btn20.Click += clickHandler;
        btn21.Click += clickHandler;
        btn22.Click += clickHandler;
        btn23.Click += clickHandler;
        btn24.Click += clickHandler;
        btn25.Click += clickHandler;
        btn26.Click += clickHandler;
    }

    private void clickHandler(object sender, EventArgs e)
    {
        if (btnCase.Text == "")
        {
            btnCase.Visible = true;
            // Selected button.Visible = false;
        }
        else
        {
            MessageBox.Show(myGame.showAmount().ToString());
            myGame.removeAmount();
        }
    }

1 个答案:

答案 0 :(得分:5)

而不是

// Selected button.Visible = false; 

编写以下代码

((Button)sender).Visible = false;