C# WinForms 中的切换按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/282118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:29:21  来源:igfitidea点击:

ToggleButton in C# WinForms

c#winformsbutton

提问by Jon Tackabury

Is it possible to create a toggle button in C# WinForms? I know that you can use a CheckBox control and set it's Appearance property to "Button", but it doesn't look right. I want it to appear sunken, not flat, when pressed. Any thoughts?

是否可以在 C# WinForms 中创建切换按钮?我知道您可以使用 CheckBox 控件并将其 Appearance 属性设置为“Button”,但它看起来不正确。我希望它在按下时看起来凹陷,而不是平坦的。有什么想法吗?

采纳答案by Jon Tackabury

I ended up overriding the OnPaint and OnBackgroundPaint events and manually drawing the button exactly like I need it. It worked pretty well.

我最终覆盖了 OnPaint 和 OnBackgroundPaint 事件,并按照我的需要手动绘制了按钮。它工作得很好。

回答by HanClinto

You can always code your own button with custom graphics and a PictureBox, though it won't necessarily match the Windows theme of your users.

您始终可以使用自定义图形和图片框编写自己的按钮,尽管它不一定与您用户的 Windows 主题相匹配。

回答by Stanislav Kniazev

Check FlatStyle property. Setting it to "System" makes the checkbox sunken in my environment.

检查 FlatStyle 属性。将其设置为“系统”会使复选框在我的环境中下沉。

回答by C. Dragon 76

You may also consider the ToolStripButton control if you don't mind hosting it in a ToolStripContainer. I think it can natively support pressed and unpressed states.

如果您不介意将 ToolStripButton 控件托管在 ToolStripContainer 中,您也可以考虑它。我认为它本身可以支持按下和未按下状态。

回答by Simon Sabin

You can just use a CheckBoxand set its appearance to Button:

您可以只使用 aCheckBox并将其外观设置为Button

CheckBox checkBox = new System.Windows.Forms.CheckBox(); 
checkBox.Appearance = System.Windows.Forms.Appearance.Button; 

回答by legendJSLC

thers is a simple way to create toggle button. I test it in vs2010. It's perfect.

这是一种创建切换按钮的简单方法。我在vs2010中测试过。这是完美的。

ToolStripButton has a "Checked" property and a "CheckOnClik" property. You can use it to act as a toggle button

ToolStripButton 有一个“Checked”属性和一个“CheckOnClik”属性。您可以将其用作切换按钮

tbtnCross.CheckOnClick = true;

OR

或者

    tbtnCross.CheckOnClick = false;
    tbtnCross.Click += new EventHandler(tbtnCross_Click);
    .....

    void tbtnCross_Click(object sender, EventArgs e)
    {
        ToolStripButton target = sender as ToolStripButton;
        target.Checked = !target.Checked;
    }

also, You can create toggle button list like this:

此外,您可以像这样创建切换按钮列表:

        private void Form1_Load(object sender, EventArgs e)
    {
        arrToolView[0] = tbtnCross;
        arrToolView[1] = tbtnLongtitude;
        arrToolView[2] = tbtnTerrain;
        arrToolView[3] = tbtnResult;
        for (int i = 0; i<arrToolView.Length; i++)
        {
            arrToolView[i].CheckOnClick = false;
            arrToolView[i].Click += new EventHandler(tbtnView_Click);
        }
        InitTree();
    }

    void tbtnView_Click(object sender, EventArgs e)
    {
        ToolStripButton target = sender as ToolStripButton;
        if (target.Checked) return;
        foreach (ToolStripButton btn in arrToolView)
        {
                btn.Checked = false;
                //btn.CheckState = CheckState.Unchecked;
        }
        target.Checked = true;
        target.CheckState = CheckState.Checked;

    }

回答by CoffeeLab

Changing a CheckBox appearance to Button will give you difficulty in adjustments. You cannot change its dimensions because its size depends on the size of your text or image.

将 CheckBox 外观更改为 Button 会使您难以进行调整。您无法更改其尺寸,因为其尺寸取决于文本或图像的尺寸。

You can try this: (initialize the countvariable first to 1 | int count = 1)

你可以试试这个:(先将count变量初始化为1 | int count = 1)

private void settingsBtn_Click(object sender, EventArgs e)
    {
        count++;

        if (count % 2 == 0)
        {
            settingsPanel.Show();
        }
        else
        {
            settingsPanel.Hide();
        }
    }

It's very simple but it works.

这很简单,但很有效。

Warning:This will work well with buttons that are occasionally used (i.e. settings), the value of count in int/longmay be overloaded when used more than it's capacity without closing the app's process. (Check data type ranges: http://msdn.microsoft.com/en-us/library/s3f49ktz.aspx)

警告:这适用于偶尔使用的按钮(即设置),当在不关闭应用程序进程的情况下使用超过其容量时,int/long 中的计数值可能会过载。(检查数据类型范围:http: //msdn.microsoft.com/en-us/library/s3f49ktz.aspx

The Good News:If you're running an app that is not intended for use 24/7 all-year round, I think this is helpful. Important thing is that when the app's process ended and you run it again, the countwill reset to 1.

好消息:如果您运行的应用程序不是全年 24/7 全天候使用的,我认为这很有帮助。重要的是,当应用程序的进程结束并再次运行时,计数将重置为 1。

回答by JRO

How about this?

这个怎么样?

Assuming you have System.Windows.Forms referenced.

假设您引用了 System.Windows.Forms。

var cbtnToggler = new CheckBox();
cbtnToggler.Appearance = Appearance.Button;
cbtnToggler.TextAlign = ContentAlignment.MiddleCenter;
cbtnToggler.MinimumSize = new Size(75, 25); //To prevent shrinkage!

Hope this helps ;)

希望这可以帮助 ;)

回答by H2Five

use if command to check status and let operate as a toggle button

使用 if 命令检查状态并让操作作为切换按钮

private void Protection_ON_OFF_Button_Click(object sender, EventArgs e)
        {

            if (FolderAddButton.Enabled == true)
            {
                FolderAddButton.Enabled = false;
            }
            else
            {
                FolderAddButton.Enabled = true;
            }
        }

回答by Anthony

This is my simple codes I hope it can help you

这是我的简单代码,希望对您有所帮助

private void button2_Click(object sender, EventArgs e)
    {
        if (button2.Text == "ON")
        {
            panel_light.BackColor = Color.Yellow; //symbolizes light turned on

            button2.Text = "OFF";
        }

        else if (button2.Text == "OFF")
        {
            panel_light.BackColor = Color.Black; //symbolizes light turned off

            button2.Text = "ON";
        }
    }