개발팁2019. 3. 28. 14:41

ToggleButton 은 버튼의 눌림을 표현 하는 방법으로,

한번 누르면 눌림상태로, 다시 누르면, 튀어 나온 상태를 말한다.


[Winform]

 

System.Windows.Forms 에는 별도의 ToggleButton 이 없고, 일반 Button 은 ToggleButton 처럼 사용할 수 없다.

 

System.Windows.Forms 의 CheckBox 나 RadioButton 의 Appearance 속성을 이용하여, ToggleButton 으로 사용할 수 있다.

 

일단, Panel 위에 CheckBox 나 RadioButton 을 올려놓는다.

 

한 Container 위에서, CheckBox 는 여러개 선택이 가능하고, RadioButton 은 단 1개만 선택이 가능하다.

 

 

CheckBox 나 RadioButton Control의 속성을 보면,

Appearance 의 기본값 Normal 을 Button 으로 바꿔준다.

 

 

이제 버튼의 모양도 동일하게 보여지게 된다.

 

 

System.Windows.Forms
CheckBox

버튼의 눌림은 .Checked = true, 눌림해제는 .Checked = false 로 체크박스나 라디오버튼으로 그대로 사용하며,

화면에 보여지는 형식만 다르다는것을 알 수 있다.

 

 

[WPF]

 

WPF 에는 ToggleButton 컨트롤이 있어서, 바로 사용이 가능하다.

 

<ToggleButton Height="25" Content="Level 1" IsChecked="{Binding level1_checked, UpdateSourceTrigger=PropertyChanged}" Checked="ShowScore"></ToggleButton>

 

Winform 에서 Checkbox 로 토글버튼을 만든것과 동일한 결과를 얻을수 있다.

 

추가적으로, RadioButton 으로 컨테이너 또는 그룹 안에서 하나의 토글 버튼만 선택이 가능하게 하기 위해서는,

Winform 처럼 RadioButton 으로 구현하면 된다.

 

WPF 의 RadioButton 컨트롤의 Style 속성을 지정하여, 토글버튼으로 보여지게 된다.

 

Style="{StaticResource {x:Type ToggleButton}}"

 

<RadioButton Height="25" Content="Level 1" Style="{StaticResource {x:Type ToggleButton}}" IsChecked="{Binding level1_checked, UpdateSourceTrigger=PropertyChanged}"></RadioButton>
<RadioButton Height="25" Content="Level 2" Style="{StaticResource {x:Type ToggleButton}}" IsChecked="{Binding level2_checked, UpdateSourceTrigger=PropertyChanged}"></RadioButton>
<RadioButton Height="25" Content="Level 3" Style="{StaticResource {x:Type ToggleButton}}" IsChecked="{Binding level3_checked, UpdateSourceTrigger=PropertyChanged}"></RadioButton>
<RadioButton Height="25" Content="Level 4" Style="{StaticResource {x:Type ToggleButton}}" IsChecked="{Binding level4_checked, UpdateSourceTrigger=PropertyChanged}"></RadioButton>
<RadioButton Height="25" Content="Level 5" Style="{StaticResource {x:Type ToggleButton}}" IsChecked="{Binding level5_checked, UpdateSourceTrigger=PropertyChanged}"></RadioButton>

 

WPF RadioButton 을 이용한 토글버튼

 

Posted by 헝개
개발팁2017. 11. 1. 14:39

C# WinForm 의 GroupBox 컨트롤의 title 영역에는 text 가 표시가 되지만,

 

이부분에 Checkbox 를 넣고, 체크박스가 선택된 경우에 GroupBox 가 활성화 되도록 하는 방법이다.

 

Form 안에 GroupBox 와 CheckBox 컨트를을 각각 1개씩 생성한다.

 

GroupBox 의 Text 값은 비워둔다.

CheckBox 의 Text 값은 적당한 값으로 적어준다.

 

CheckBox 의 위치는 아무곳에나 두어도 상관이 없다.

 

CheckBox Control 에서 마우스 오른쪽 버튼을 눌러서,

 

맨앞으로 가져오기 를 한번 클릭해준다.

 

그래야, GroupBox 에 CheckBox 가 감춰지지 않고 잘 보여지게 된다.

 

그리고, FORM 의 생성자를 아래와 같이 정의하자

 

public CustomForm()
{

    InitializeComponent();


    // 이부분을 추가한다.
    this.chkImageChange.Location = new Point(this.group_image.Location.X + 13, this.group_image.Location.Y - 1);
    this.chkVideoChange.Location = new Point(this.group_video.Location.X + 13, this.group_video.Location.Y - 1);

 

이부분은, CheckBox 컨트롤의 위치를 GroupBox컨트를의 title 영역위에 위치하도록 해주는 부분다.

 

이제 CheckBox 컨트롤의 이벤트를 추가할 것이다.

 

CheckedChanged 이벤트를 아래와 같이 정의한다.

 

private void chkImageChange_CheckedChanged(object sender, EventArgs e)
{
    this.group_image.Enabled = this.chkImageChange.Checked;

 

CheckBox 컨트롤을 체크하면, 그룹박스가 활성화되고, 해제되면, 그룹박스도 비활성화 될것이다.

 

이제, Form 에서 기본값으로 활성화 할지, 비활성화 할지를 선택하면 된다.

 


실행 화면 : 아래와 같은 형태로 나올 것이다.


 

 

 

Posted by 헝개