c# - How to implement a graphical 3-Way "Switch" -
i'm trying build 3-way "switch" in winforms project.
it sends 1 command 3 "settings", should alternate between 3 different background images each time user clicks on button. i've implemented 2-way toggle switch project using checkbox it's appearance set "button", don't believe method work 3-way switch.
here code i've tried, doesn't seem when button clicked:
private void threewaybutton_click(object sender, eventargs e) { if (threewaybutton.backgroundimage.equals(properties.resources.threeway_1)) { threewaybutton.backgroundimage = properties.resources.threeway_2; } else if (threewaybutton.backgroundimage.equals(properties.resources.threeway_2)) { threewaybutton.backgroundimage = properties.resources.threeway_3; } else if (threewaybutton.backgroundimage.equals(properties.resources.threeway_3)) { threewaybutton.backgroundimage = properties.resources.threeway_1; } } another method tried using switch:
static int switch_state = 0; //... protected void threewaybutton_click(object sender, eventargs e) { switch_state++; switch (switch_state) { case 1: threewaybutton.backgroundimage = properties.resources.threeway_2; break; case 2: threewaybutton.backgroundimage = properties.resources.threeway_3; break; case 3: threewaybutton.backgroundimage = properties.resources.threeway_1; break; default: break; } } this method kind of works; cycles through 3 images, once gets last one, doesn't cycle through images again.
if second method appropriate 1 use, i'd revert case 1 after user clicks on button when switch_state case 3
it should cycle between 3 images each time user clicks button, no matter how many times button has been clicked.
your second approach one, need add:
if(switch_state > 3) switch_state = 1; just after switch_state++, else continue incrementing doing nothing.
Comments
Post a Comment