ラジオボタンについて

この章ではラジオボタンについて解説します。

.NET Frameworkにおいてラジオボタンを表すクラスはRadioButtonです。
下記はラジオボタンを使ったサンプルコードです。

using System;
using System.Drawing;
using System.Windows.Forms;

public class Sample
{
    public static void Main(String[] args)
    {
        Form f = new Form();
        RadioButton radioButton = new RadioButton();
        radioButton.Location = new Point(10, 10);
        radioButton.Text = "ラジオボタン";
        f.Controls.Add(radioButton);
        Application.Run(f);
    }
}

サンプルを実行するとウィンドウが表示され、その中にラジオボタンが表示されます。

通常ラジオボタンは複数の選択肢の中から一つ選ぶ場合に使用します。
そのラジオボタンにグループを作るにはGroupBoxクラスを使用します。

using System;
using System.Drawing;
using System.Windows.Forms;

public class Sample
{
    public static void Main(String[] args)
    {
        Form f = new Form();
        RadioButton radioButton1 = new RadioButton();
        radioButton1.Location = new Point(10, 10);
        radioButton1.Text = "ラジオボタン1";
        RadioButton radioButton2 = new RadioButton();
        radioButton2.Location = new Point(10, 40);
        radioButton2.Text = "ラジオボタン2";
        
        GroupBox groupBox = new GroupBox();
        groupBox.Controls.Add(radioButton1);
        groupBox.Controls.Add(radioButton2);
        
        f.Controls.Add(groupBox);
        Application.Run(f);
    }
}

RadioButtonクラスの主なプロパティ

チェック状態

ラジオボタンでチェック状態を取得するにはCheckedプロパティを使用します。

項目名

ラジオボタンの項目名を取得・参照するにはTextプロパティを使用します。

表示位置

ラジオボタンの位置を設定するにはLocationプロパティを設定します。
Locationプロパティの型はPointクラスになります。



inserted by FC2 system