Efficient way to fire 2 diff events with the same button on each click?
I have a C# button, let's开发者_如何学JAVA say 'ON/OFF':
I want to change 'OFF', when we first click it and run buttonOFF() When we click again, it changes to 'ON' and run buttonON()
and so on..
What's the correct way to do that? Can I do it all in one onClick event? peace of small example would be useful. Thanks !
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();
}
}
You might best using this method. Using a checkbox with a button appearance
CheckBox checkBox1 = new System.Windows.Forms.CheckBox();
checkBox1.Appearance = System.Windows.Forms.Appearance.Button;
Although the solution works, I think there's an issue with the UI. Consider this:
As a user I'm looking at a button, the button says "Off". Does this represent state or action? Is it on and by clicking I'll turn it off? Or is it off and by clicking I'll turn it on? Also, what am I turning on/off? The UI is somewhat ambiguous unless I have experience and have learned what it means.
However, if you were to use something like the checkbox appearance suggested by Dean and label the button (checkbox), say, "Engine", both the state and the purpose are clear:
精彩评论