UFO ET IT

C # WinForms의 ToggleButton

ufoet 2020. 11. 29. 12:42
반응형

C # WinForms의 ToggleButton


C # WinForms에서 토글 버튼을 만들 수 있습니까? CheckBox 컨트롤을 사용하고 Appearance 속성을 "Button"으로 설정할 수 있다는 것을 알고 있지만 제대로 보이지 않습니다. 누를 때 평평하지 않고 움푹 들어간 것처럼 보이기를 원합니다. 이견있는 사람?


a CheckBox를 사용하고 모양을 Button다음 과 같이 설정할 수 있습니다 .

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

FlatStyle 속성을 확인하십시오. "시스템"으로 설정하면 내 환경에서 확인란이 가라 앉습니다.


이건 어때?

System.Windows.Forms가 참조되었다고 가정합니다.

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

도움이 되었기를 바랍니다 ;)


thers는 토글 버튼을 만드는 간단한 방법입니다. vs2010에서 테스트합니다. 그것은 완벽.

ToolStripButton에는 "Checked"속성과 "CheckOnClik"속성이 있습니다. 토글 버튼으로 사용할 수 있습니다.

tbtnCross.CheckOnClick = true;

또는

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

또한 다음과 같이 토글 버튼 목록을 만들 수 있습니다.

        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;

    }

ToolStripContainer에서 호스팅해도 괜찮은 경우 ToolStripButton 컨트롤을 고려할 수도 있습니다. 기본적으로 눌러 진 상태와 누르지 않은 상태를 지원할 수 있다고 생각합니다.


이것은 내 간단한 코드입니다.

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

사용자의 Windows 테마와 반드시 일치 할 필요는 없지만 사용자 지정 그래픽과 PictureBox를 사용하여 언제든지 고유 한 단추를 코딩 할 수 있습니다.


결국 OnPaint 및 OnBackgroundPaint 이벤트를 재정의하고 필요한 것처럼 버튼을 수동으로 그렸습니다. 꽤 잘 작동했습니다.


if 명령을 사용하여 상태를 확인하고 토글 버튼으로 작동합니다.

private void Protection_ON_OFF_Button_Click(object sender, EventArgs e)
        {

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

CheckBox 모양을 Button으로 변경하면 조정이 어려워집니다. 크기는 텍스트 또는 이미지의 크기에 따라 달라 지므로 크기를 변경할 수 없습니다.

이것을 시도해 볼 수 있습니다 : ( 먼저 카운트 변수를 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.

참고URL : https://stackoverflow.com/questions/282118/togglebutton-in-c-sharp-winforms

반응형