在每次点击时使用相同按钮触发2个差异事件的有效方法?

Ste*_*fin 1 c# button

我有一个C#按钮,让我们说'开/关':

我想改变'OFF',当我们第一次点击它并运行buttonOFF()当我们再次点击时,它会变为'ON'并运行buttonON()

等等..

这样做的正确方法是什么?我可以在一个onClick事件中完成所有操作吗?和平的小榜样会很有用.谢谢 !

Vil*_*lx- 5

private void buttonON()
{
    // Magic here
}

private void buttonOFF()
{
    // Magic here
}

protected void button_click(object sender, EventArgs e)
{
    if ( button.Text == "ON" )
    {
        button.Text = "OFF";
        this.buttonOFF();
    }
    else
    {
        button.Text = "ON";
        this.buttonON();
    }
}
Run Code Online (Sandbox Code Playgroud)


Dea*_*ean 5

您最好使用方法.使用带按钮外观的复选框

CheckBox checkBox1 = new System.Windows.Forms.CheckBox(); 
checkBox1.Appearance = System.Windows.Forms.Appearance.Button; 
Run Code Online (Sandbox Code Playgroud)