Radio Button & GroupBox

Radio Button を GroupBox で囲みます。

前田稔(Maeda Minoru)の超初心者のプログラム入門

プログラムの説明

  1. 一般的にラジオボタンは、複数個並べた中から一個だけ選択出来ます。
    今回はラジオボタンをグループ化して、花の種類と花の色を選択してみましょう。
    グループ化すると、花と色から一個ずつ選択出来るようになります。
    /*******************************************/
    /*★ ラジオボタンをグループ化    前田 稔 ★*/
    /*******************************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyForm : Form
    {
        Label label;
        RadioButton[] radioButton1 = new RadioButton[4];
        RadioButton[] radioButton2 = new RadioButton[3];
        GroupBox groupBox1; // グループボックス1
        GroupBox groupBox2; // グループボックス2
        string color;
        string flower;
    
        public MyForm()
        {
            label = new Label();
            label.AutoSize = true;
            label.Location = new Point(20, 240);
    
            string[] item1 = { "チューリップ", "コスモス", "シクラメン", "ゆり" };
            for (int i = 0; i < 4; i++)
            {
                radioButton1[i] = new RadioButton();
                radioButton1[i].Text = item1[i];
                radioButton1[i].Left = 10;
                radioButton1[i].Top = i * 22 + 20;
                radioButton1[i].CheckedChanged +=
                    new EventHandler(radioButton_CheckedChanged1);
            }
            radioButton1[0].Checked = true;  // 最初は 'チューリップ' を選択
    
            string[] item2 = { "赤", "白", "黄" };
            for (int i = 0; i < 3; i++)
            {
                radioButton2[i] = new RadioButton();
                radioButton2[i].Text = item2[i];
                radioButton2[i].Left = 10;
                radioButton2[i].Top = i * 22 + 20;
                radioButton2[i].CheckedChanged +=
                    new EventHandler(radioButton_CheckedChanged2);
            }
            radioButton2[0].Checked = true;  // 最初は '赤' を選択
    
            groupBox1 = new GroupBox();
            groupBox1.Text = "花";
            groupBox1.Location = new Point(20, 10);
            groupBox1.Size = new Size(160, 120);
            groupBox1.Controls.AddRange(radioButton1);
    
            groupBox2 = new GroupBox();
            groupBox2.Text = "色";
            groupBox2.Location = new Point(20, 140);
            groupBox2.Size = new Size(160, 90);
            groupBox2.Controls.AddRange(radioButton2);
    
            this.Controls.Add(label);
            this.Controls.Add(groupBox1);
            this.Controls.Add(groupBox2);
        }
    
        // 花が変更されたときのイベントハンドラ
        void radioButton_CheckedChanged1(object sender, EventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;
            if (radioButton.Checked)
            {
                flower = radioButton.Text;
                label.Text = string.Format("花:" + flower + "  色:" + color);
            }
        }
    
        // 色が変更されたときのイベントハンドラ
        void radioButton_CheckedChanged2(object sender, EventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;
            if (radioButton.Checked)
            {
                color = radioButton.Text;
                label.Text = string.Format("花:" + flower + "  色:" + color);
            }
        }
    }
    
    class form01
    {
        public static void Main()
        {
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    
  2. ラジオボタンをグループ化したときの Left と Top は、グループ内の相対位置です。
        radioButton2[i].Left = 10;
        radioButton2[i].Top = i * 22 + 20;
    
  3. イベントハンドラの設定です。
    花が選択されたときは radioButton_CheckedChanged1 を、色が選択されたときは radioButton_CheckedChanged2 を設定します。
        radioButton1[i].CheckedChanged +=
            new EventHandler(radioButton_CheckedChanged1);
    
        radioButton2[i].CheckedChanged +=
            new EventHandler(radioButton_CheckedChanged2);
    
  4. 花が選択されたときのイベントハンドラです。
    花の種類を flower に保存して、flower と color を表示します。
        void radioButton_CheckedChanged1(object sender, EventArgs e)
        {   RadioButton radioButton = sender as RadioButton;
            if (radioButton.Checked)
            {
                flower = radioButton.Text;
                label.Text = string.Format("花:" + flower + "  色:" + color);
            }
        }
    
  5. 色が選択されたときのイベントハンドラです。
    色を color に保存して、flower と color を表示します。
        // 色が変更されたときのイベントハンドラ
        void radioButton_CheckedChanged2(object sender, EventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;
            if (radioButton.Checked)
            {
                color = radioButton.Text;
                label.Text = string.Format("花:" + flower + "  色:" + color);
            }
        }
    
  6. 選択されているラジオボタンの Index を調べる方法です。
        int i;
        for(i=0; i<3; i++)
        {   if (radioButton[i].Checked) break;
        }
        if (i<3)
        {   // i が選択されているラジオボタンです。
        }
    
    アプリケーションの例は囲碁ソフトの RadioButton を参照して下さい。

自動生成

  1. [空のプロジェクト]が選択出来なくなったので、自動生成のプロジェクトで構築する方法も説明します。
    プロジェクトの作成は 自動生成を使って Form を作成する を参照して下さい。
  2. [デザイン]画面を表示して[ツールボックス]から[GroupBox]を左右に配置します。
    左には4個の、右には3個の RadioButton を配置するのでサイズを調整して下さい。
  3. 左の GroupBox の Text に"花"を設定して4個の RadioButton を配置します。
    右の GroupBox の Text に"色"を設定して3個の RadioButton を配置します。
    "花"の RadioButton の Click イベントに redioButtonClick1 を設定します。
    "色"の RadioButton の Click イベントに redioButtonClick2 を設定します。
  4. GroupBox の下に TextBox を貼り付けます。
    GroupBox の規定値の名前は textBox1 で、ここに花の種類と色を表示します。
  5. Form1.cs の class Form1 に花の種類と色を格納する string 領域を定義します。
    花には "チューリップ" を、色には "赤" を設定します。
  6. redioButtonClick1 では、花の種類を取得して TextBox に表示します。
    redioButtonClick2 では、花の色を取得して TextBox に表示します。
  7. Form1.cs のソースコードです。
        public partial class Form1 : Form
        {
            string flower = "チューリップ";
            string color = "赤";
    
            public Form1()
            {
                InitializeComponent();
                radioButton1.Checked = true;  // 最初は 'チューリップ' を選択
                radioButton5.Checked = true;  // 最初は '赤' を選択
            }
    
            private void redioButtonClick1(object sender, EventArgs e)
            {
                RadioButton radioButton = sender as RadioButton;
                flower = radioButton.Text;
                textBox1.Text = string.Format(color + "い色の" + flower + "の花");
            }
    
            private void redioButtonClick2(object sender, EventArgs e)
            {
                RadioButton radioButton = sender as RadioButton;
                color = radioButton.Text;
                textBox1.Text = string.Format(color + "い色の" + flower + "の花");
            }
        }
    
  8. GroupBox や RadioButton は Form1.Designer.cs で定義されています。
    RadioButton を配列で定義するときは、ここを修正して下さい。

超初心者のプログラム入門(C# Frame Work)