Search code examples
togglebutton

Change sequence of states on a three-state ToggleButton


It seems that the out-of-box sequence for a three-state ToggleButton is On, Indeterminate, Off.

I would like to change this to On, Off, Indeterminate; similar to the question presented here .

I've tried modifying my StateChanging event, but I think that will result in an infinite loop.


Solution

  • I briefly implemented a working solution using ToggleStateChanging to change the order of the states, and then using a global variable to avoid an infinite loop. However, I then had a new problem where the ToggleButton had some sort of auto-theme where it was shaded differently for each state and on MouseHover. I didn't want that theme, so I ultimately just changed the buttons to standard buttons, and now I'm using the buttons' Tag property to emulate ToggleState.

    My particular solution uses a Telerik RadButton, but this would work for a standard button as well on a WinForm.

    private void myButton_Click(object sender, EventArgs e)
    {
        RadButton myButton = (RadButton)sender;
    
        switch (myButton.Tag.ToString())
        {
            case "Indeterminant":
                myButton.Tag = "On";
                break;
            case "On":
                myButton.Tag = "Off";
                break;
            case "Off":
                myButton.Tag = "Indeterminant";
                break;
            default:
                break;
        }
    }