Windows Form 라디오 단추를 그룹화하는 방법
Windows Form 응용 프로그램에서 라디오 단추를 그룹화하려면 어떻게 해야 합니까(ASP.NET의 라디오 단추 목록처럼!)?
따라서 옵션에서 선택한 각 사례를 전환할 수 있습니다.
그룹의 모든 라디오 단추를 컨테이너 개체에 넣습니다(예:Panel또는GroupBox그러면 Windows Forms에서 자동으로 그룹화됩니다.
그룹 상자에 라디오 단추를 배치하는 방법을 살펴봅니다.
그룹의 모든 라디오 버튼을 같은 컨테이너 안에 배치해야 합니다. 예를 들어,GroupBox또는Panel.
저는 WPF에서 라디오 버튼을 그룹화하는 개념을 좋아합니다.속성이 있습니다.GroupName상호 배타적인 RadioButton 컨트롤을 지정합니다(http://msdn.microsoft.com/de-de/library/system.windows.controls.radiobutton.aspx) .
그래서 이 기능을 지원하는 WinForms용 파생 클래스를 작성했습니다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Windows.Forms.VisualStyles;
using System.Drawing;
using System.ComponentModel;
namespace Use.your.own
{
public class AdvancedRadioButton : CheckBox
{
public enum Level { Parent, Form };
[Category("AdvancedRadioButton"),
Description("Gets or sets the level that specifies which RadioButton controls are affected."),
DefaultValue(Level.Parent)]
public Level GroupNameLevel { get; set; }
[Category("AdvancedRadioButton"),
Description("Gets or sets the name that specifies which RadioButton controls are mutually exclusive.")]
public string GroupName { get; set; }
protected override void OnCheckedChanged(EventArgs e)
{
base.OnCheckedChanged(e);
if (Checked)
{
var arbControls = (dynamic)null;
switch (GroupNameLevel)
{
case Level.Parent:
if (this.Parent != null)
arbControls = GetAll(this.Parent, typeof(AdvancedRadioButton));
break;
case Level.Form:
Form form = this.FindForm();
if (form != null)
arbControls = GetAll(this.FindForm(), typeof(AdvancedRadioButton));
break;
}
if (arbControls != null)
foreach (Control control in arbControls)
if (control != this &&
(control as AdvancedRadioButton).GroupName == this.GroupName)
(control as AdvancedRadioButton).Checked = false;
}
}
protected override void OnClick(EventArgs e)
{
if (!Checked)
base.OnClick(e);
}
protected override void OnPaint(PaintEventArgs pevent)
{
CheckBoxRenderer.DrawParentBackground(pevent.Graphics, pevent.ClipRectangle, this);
RadioButtonState radioButtonState;
if (Checked)
{
radioButtonState = RadioButtonState.CheckedNormal;
if (Focused)
radioButtonState = RadioButtonState.CheckedHot;
if (!Enabled)
radioButtonState = RadioButtonState.CheckedDisabled;
}
else
{
radioButtonState = RadioButtonState.UncheckedNormal;
if (Focused)
radioButtonState = RadioButtonState.UncheckedHot;
if (!Enabled)
radioButtonState = RadioButtonState.UncheckedDisabled;
}
Size glyphSize = RadioButtonRenderer.GetGlyphSize(pevent.Graphics, radioButtonState);
Rectangle rect = pevent.ClipRectangle;
rect.Width -= glyphSize.Width;
rect.Location = new Point(rect.Left + glyphSize.Width, rect.Top);
RadioButtonRenderer.DrawRadioButton(pevent.Graphics, new System.Drawing.Point(0, rect.Height / 2 - glyphSize.Height / 2), rect, this.Text, this.Font, this.Focused, radioButtonState);
}
private IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
}
}
패널이 없는 라디오 버튼
public class RadioButton2 : RadioButton
{
public string GroupName { get; set; }
}
private void RadioButton2_Clicked(object sender, EventArgs e)
{
RadioButton2 rb = (sender as RadioButton2);
if (!rb.Checked)
{
foreach (var c in Controls)
{
if (c is RadioButton2 && (c as RadioButton2).GroupName == rb.GroupName)
{
(c as RadioButton2).Checked = false;
}
}
rb.Checked = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
//a group
RadioButton2 rb1 = new RadioButton2();
rb1.Text = "radio1";
rb1.AutoSize = true;
rb1.AutoCheck = false;
rb1.Top = 50;
rb1.Left = 50;
rb1.GroupName = "a";
rb1.Click += RadioButton2_Clicked;
Controls.Add(rb1);
RadioButton2 rb2 = new RadioButton2();
rb2.Text = "radio2";
rb2.AutoSize = true;
rb2.AutoCheck = false;
rb2.Top = 50;
rb2.Left = 100;
rb2.GroupName = "a";
rb2.Click += RadioButton2_Clicked;
Controls.Add(rb2);
//b group
RadioButton2 rb3 = new RadioButton2();
rb3.Text = "radio3";
rb3.AutoSize = true;
rb3.AutoCheck = false;
rb3.Top = 80;
rb3.Left = 50;
rb3.GroupName = "b";
rb3.Click += RadioButton2_Clicked;
Controls.Add(rb3);
RadioButton2 rb4 = new RadioButton2();
rb4.Text = "radio4";
rb4.AutoSize = true;
rb4.AutoCheck = false;
rb4.Top = 80;
rb4.Left = 100;
rb4.GroupName = "b";
rb4.Click += RadioButton2_Clicked;
Controls.Add(rb4);
}
GroupBox(또는 다른 패널) 안에 라디오 버튼 삽입
공유 컨테이너 내부의 모든 라디오 버튼은 기본적으로 동일한 그룹에 있습니다.즉, 선택한 항목 중 하나를 선택하면 다른 항목은 선택되지 않습니다.독립적인 라디오 단추 그룹을 만들려면 다음과 같은 다른 컨테이너에 배치해야 합니다.Group Box또는 코드 뒤에 있는 코드를 통해 체크됨 상태를 제어합니다.
GroupBox더 낫습니다.하지만 그룹 박스 뿐만 아니라, 심지어 당신도 사용할 수 있습니다.Panels(System.Windows.Forms.Panel).
- 이것은 인터넷 프로토콜 버전 4 설정 대화상자를 설계할 때 매우 유용합니다.(PC(윈도우)에서 확인하면 동작을 이해할 수 있습니다.)
한 컨테이너에 넣을 수 없는 경우 각 라디오 버튼의 선택된 상태를 변경하기 위해 코드를 작성해야 합니다.
private void rbDataSourceFile_CheckedChanged(object sender, EventArgs e)
{
rbDataSourceNet.Checked = !rbDataSourceFile.Checked;
}
private void rbDataSourceNet_CheckedChanged(object sender, EventArgs e)
{
rbDataSourceFile.Checked = !rbDataSourceNet.Checked;
}
언급URL : https://stackoverflow.com/questions/2178240/how-do-i-group-windows-form-radio-buttons
'programing' 카테고리의 다른 글
| 호스트에 대한 nopg_syslog.conf 항목 (0) | 2023.05.26 |
|---|---|
| -ObjC 링커 플래그의 역할은 무엇입니까? (0) | 2023.05.26 |
| VB.net 에 증분 연산자가 없습니다. (0) | 2023.05.26 |
| 여러 개의 .gitignore가 눈살을 찌푸리고 있습니까? (0) | 2023.05.26 |
| 특정 리비전/변경 세트로 git 저장소를 복제하는 방법은 무엇입니까? (0) | 2023.05.26 |
