문제

나는 그것을 알고있다 WebForms a RadioButtonList 통제이지만 하나는 찾을 수 없습니다 WinForms. 내가 필요한 것은 3 개의 radiobuttons를 함께 그룹화하여 한 번에 1 개만 선택할 수 있도록하는 것입니다. 나는 코드를 통해 이것을해야한다는 것을 알고 있습니다. 그것은 고통입니다. 나는 단지 보지 못한다 RadioButtonList 어딘가에 있거나 실제로 존재하지 않습니다 WinForms?

도움이 되었습니까?

해결책

분명히.

그룹 박스 또는 패널을 사용하여 3 개의 radiobutton을 함께 그룹화 할 수 있습니다. 여기서 수행 된대로.

다른 팁

라디오 버튼을 그룹화하려면 컨테이너에 넣는 것이 충분하면 그룹처럼 작동하지만 방법과 같은 데이터 바인딩이 필요한 경우 ComboBox 또는 ListBox 또는 CheckedListBox 작품, 당신은 필요합니다 RadioButtonList 제어.

Windows 양식에는 내장이 없습니다 RadioButtonList 제어. 양식을 도출하여 자신의 컨트롤을 만들 수 있습니다 ListBox 그리고 소유자 드로우와 라디오 버튼을 직접 그립니다. 이것이 방법입니다 CheckedListBox 또한 만들어졌습니다.

이런 식으로 컨트롤은 데이터 바인딩을 지원하며 ListBox, 포함 DataSource, SelectedValue, DisplayMember, ValueMember 등등. 예를 들어이 방법으로 간단히 사용할 수 있습니다.

this.radioButtonList1.DataSource = peopleTable; 
this.radioButtonList1.DisplayMember = "Name"; 
this.radioButtonList1.ValueMember= "Id";

또는 예를 들어 enum, 이런 식으로 :

this.radioButtonList1.DataSource = Enum.GetValues(typeof(DayOfWeek)); 

아래 이미지에서 두 번째 이미지에서 RadioButtonList 설정하여 비활성화됩니다 Enabled = false;:

enter image description here enter image description here

또한 컨트롤은 오른쪽에서 왼쪽을 지원합니다.

enter image description here

또한 멀티 컬럼을 지원합니다.

enter image description here

Radiobuttonlist

다음은 제어를위한 소스 코드입니다. 당신은 그것을 평범한 것처럼 사용할 수 있습니다 ListBox 데이터 바인딩을 사용하거나 사용하지 않고 항목을 추가하거나 데이터 소스를 설정함으로써 :

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
public class RadioButtonList : ListBox
{
    Size s;
    public RadioButtonList()
    {
        this.DrawMode = DrawMode.OwnerDrawFixed;
        using (var g = Graphics.FromHwnd(IntPtr.Zero))
            s = RadioButtonRenderer.GetGlyphSize(
                Graphics.FromHwnd(IntPtr.Zero), RadioButtonState.CheckedNormal);
    }
    protected override void OnDrawItem(DrawItemEventArgs e)
    {

        var text = (Items.Count > 0) ? GetItemText(Items[e.Index]) : Name;
        Rectangle r = e.Bounds; Point p;
        var flags = TextFormatFlags.Default | TextFormatFlags.NoPrefix;
        var selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
        var state = selected ?
            (Enabled ? RadioButtonState.CheckedNormal : 
                       RadioButtonState.CheckedDisabled) :
            (Enabled ? RadioButtonState.UncheckedNormal : 
                       RadioButtonState.UncheckedDisabled);
        if (RightToLeft == System.Windows.Forms.RightToLeft.Yes)
        {
            p = new Point(r.Right - r.Height + (ItemHeight - s.Width) / 2,
                r.Top + (ItemHeight - s.Height) / 2);
            r = new Rectangle(r.Left, r.Top, r.Width - r.Height, r.Height);
            flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
        }
        else
        {
            p = new Point(r.Left + (ItemHeight - s.Width) / 2,
            r.Top + (ItemHeight - s.Height) / 2);
            r = new Rectangle(r.Left + r.Height, r.Top, r.Width - r.Height, r.Height);
        }
        var bc = selected ? (Enabled ? SystemColors.Highlight : 
            SystemColors.InactiveBorder) : BackColor;
        var fc = selected ? (Enabled ? SystemColors.HighlightText : 
            SystemColors.GrayText) : ForeColor;
        using (var b = new SolidBrush(bc))
            e.Graphics.FillRectangle(b, e.Bounds);
        RadioButtonRenderer.DrawRadioButton(e.Graphics, p, state);
        TextRenderer.DrawText(e.Graphics, text, Font, r, fc, bc, flags);
        e.DrawFocusRectangle();
        base.OnDrawItem(e);
    }
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override SelectionMode SelectionMode
    {
        get { return System.Windows.Forms.SelectionMode.One; }
        set { }
    }
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override int ItemHeight
    {
        get { return (this.Font.Height + 2); }
        set { }
    }
    [EditorBrowsable(EditorBrowsableState.Never), Browsable(false),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override DrawMode DrawMode
    {
        get { return base.DrawMode; }
        set { base.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; }
    }
}

여러 라디오 버튼이 동일한 컨테이너에 있다는 간단한 사실은 상호 배타적 이므로이 동작을 직접 코딩 할 필요가 없습니다. Matthew가 제안한대로 패널이나 그룹 박스에 넣으십시오.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top