C#のwinformsでラジオボタンを試す

コンピュータ

ラジオボタンの使い方を確認します。

namespace RadioSample01;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        var groupbox = new GroupBox
        {
            Size = new (300, 150),
            Text = "2値化",
            Parent = this,
        };
        var binRadioBtn = new RadioButton
        {
            Location = new Point(20, 30),
            Text = "バイナリ",
            Checked = true,
            Parent = groupbox,
        };
        var adaptiveRadioBtn = new RadioButton
        {
            Location = new Point(20, 60),
            Text = "アダプティブ",
            Checked = false,
            Parent = groupbox,
        };
        var oothRadioBtn = new RadioButton
        {
            Location = new Point(20, 90),
            Text = "大津",
            Checked = false,
            Parent = groupbox,
        };
        Action<Object?, EventArgs> check = new Action<object?, EventArgs>((s, e)=>
        {
            RadioButton? btn = s as RadioButton;
            if (btn is null) return;

            MessageBox.Show(btn.Text, "メッセージ");
        });
        binRadioBtn.Click += (s, e) => check(s, e);
        adaptiveRadioBtn.Click += (s, e) => check(s, e);
        oothRadioBtn.Click += (s, e) => check(s, e);
    }
}


コメント