Question

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?

Was it helpful?

Solution 6

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

OTHER TIPS

You can just use a CheckBox and set its appearance to Button:

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

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

How about this?

Assuming you have System.Windows.Forms referenced.

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

Hope this helps ;)

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

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

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;

    }

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.

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

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.

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

private void Protection_ON_OFF_Button_Click(object sender, EventArgs e)
        {

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

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.

You can try this: (initialize the count variable first to 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/long may 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)

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 count will reset to 1.

Well Jon Tackabury you can use MetroUI Framework to use toggle button. It provides many other controls that are looks different UI just like windows 8.

You can try it at least once.

How to use just see this, https://www.youtube.com/watch?v=Ryxew9cgkZA

I hope this is helpful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top