如何实现图形化 3-Way "Switch"

How to implement a graphical 3-Way "Switch"

我正在尝试在我的 WinForms 项目中构建一个 3-Way "Switch"。

它只为所有三个 "settings" 发送一个命令,但每次用户单击按钮时应在 3 个不同的背景图像之间切换。我已经通过使用外观设置为 "Button" 的 CheckBox 在我的项目中实现了一个 2 向切换开关,但我不认为这种方法适用于 3 向切换。

这是我试过的代码,但是当点击按钮时它似乎没有做任何事情:

    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;
        }
    }

我尝试的另一种方法是使用开关:

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;
        }
    }

这种方法有点有效;它会循环显示三张图像,但是一旦到达最后一张图像,它就不会再次循环显示图像。

如果第二种方法适合使用,我希望它在 switch_statecase 3 时用户单击按钮后恢复为 case 1

每次用户单击按钮时,无论按钮被单击了多少次,它都应该在三个图像之间循环。

你的第二种方法很好,你只需要添加:

if(switch_state > 3)
    switch_state = 1;

就在你的 switch_state++ 之后,否则它将继续递增,因此什么都不做。