【Yugongシリーズ】WinformコントロールのRadioButtonコントロールの詳細解説 2023年9月


序文

Winform コントロールは、Windows フォームのユーザー インターフェイス要素です。ボタン、ラベル、テキスト ボックス、ドロップダウン リスト ボックス、チェック ボックス、ラジオ ボックス、プログレス バーなど、Windows アプリケーションのさまざまな視覚的および対話型コンポーネントを作成するために使用できます。 . . 開発者は Winform コントロールを使用してユーザー インターフェイスを構築し、ユーザーのアクションに応答して強力なデスクトップ アプリケーションを作成できます。

1. RadioButtonコントロールの詳しい説明

RadioButton コントロールは Winform で一般的に使用されるコントロールであり、ユーザーが相互に排他的なオプションのセットからオプションを選択できるようにするために使用されます。

Winform で RadioButton コントロールを使用する手順は次のとおりです。

  1. Visual Studio を開き、新しいプロジェクトを作成し、[Windows フォーム アプリケーション] を選択します。

  2. デザイン ビューで、RadioButton コントロールをツールボックスからフォームにドラッグします。

  3. プロパティ ウィンドウを使用して、テキスト、位置、サイズ、その他のプロパティを設定できます。

  4. プロパティ ウィンドウに Name プロパティが表示されます。RadioButton コントロールに意味のある名前を付けると、コード内で参照しやすくなります。

  5. 同じオプション グループ内の RadioButton コントロールは、同じコンテナ コントロール (Panel や GroupBox など) でラップする必要があります。

  6. コードでは、Checked プロパティを使用して、RadioButton コントロールが選択されているかどうかを判断できます。のように:

if (radioButton1.Checked)
{
    
    
    // radioButton1被选中
}
else if (radioButton2.Checked)
{
    
    
    // radioButton2被选中
}

予防:

  1. RadioButton コントロールは、同じグループ内の他の RadioButton コントロールと組み合わせて使用​​する必要があります。そうしないと、相互排他が実現できません。

  2. 同一グループ内のRadioButtonコントロールのCheckedChangedイベントは、グループ内の選択判定や操作を行うことができます。

  3. RadioButton コントロールは、次のようなコードを通じて選択状態を設定することもできます。

radioButton1.Checked = true;

以上がWinformでRadioButtonコントロールを使用する方法です。

1. 属性の紹介

1.1 チェックアライン

RadioButton コントロールは、Winform で一般的に使用されるコントロールの 1 つであり、ラジオ選択機能を提供するために使用されます。CheckAlign プロパティは、RadioButton コントロールで選択した円の位置を設定するために使用されます。

CheckAlign プロパティには、次の 3 つの列挙値があります。

  1. TopLeft: 選択した円は、RadioButton コントロールの左上隅にあります。

  2. MiddleLeft: 選択した円は、RadioButton コントロールの中央の左側にあります。

  3. BottomLeft: 選択した円は、RadioButton コントロールの下部、左側にあります。

CheckAlign プロパティを使用するには、設計時またはコードで設定する必要があります。設計時に、RadioButton コントロールを選択し、プロパティ パネルで CheckAlign プロパティを見つけ、ドロップダウン メニューから対応する列挙値を設定します。コードでは、RadioButton コントロールの CheckAlign プロパティを設定することでこれを実現できます。

簡単なサンプルコードを次に示します。

RadioButton radioButton1 = new RadioButton();
radioButton1.Text = "Option 1";
radioButton1.CheckAlign = ContentAlignment.MiddleLeft;

上記のコードは、RadioButton コントロールを作成し、そのテキストを「オプション 1」に設定し、選択した円の位置を中央の左に設定します。

2. 一般的なシナリオ

RadioButton コントロールは、Winform の次のシナリオでよく使用されます。

  1. ユーザーは複数のオプションから選択する必要があり、RadioButton コントロールを使用してこれらのオプションを表示できます。
  2. フォームで RadioButton コントロールを使用すると、ユーザーは性別や婚姻状況などの個人情報を選択できます。
  3. アプリケーションの設定ページで、RadioButton コントロールを使用すると、ユーザーがオプションから項目を選択してアプリケーションの設定を変更できるようになります。
  4. アンケート アプリケーションでは、RadioButton コントロールを使用して、ユーザーが複数の選択肢から回答を選択できるようにすることができます。
  5. ゲームでは、RadioButton コントロールを使用して、プレーヤーがゲームの難易度を選択できるようにすることができます。

3. 具体的なケース

以下は、RadioButton コントロールを使用してユーザーが独自の移動モードを選択し、選択結果を表示できるようにする方法を示す完全な Winform アプリケーションです。

using System;
using System.Windows.Forms;

namespace RadioButtonDemo
{
    
    
    public partial class Form1 : Form
    {
    
    
        public Form1()
        {
    
    
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
    
    
            string vehicle = "";
            if (radioButton1.Checked)
                vehicle = "小汽车";
            else if (radioButton2.Checked)
                vehicle = "公交车";
            else if (radioButton3.Checked)
                vehicle = "地铁";
            else if (radioButton4.Checked)
                vehicle = "自行车";

            MessageBox.Show("您选择的是:" + vehicle);
        }
    }
}

このアプリケーションでは、まずフォームに 4 つの RadioButton コントロールを追加します。これらは、「車」、「バス」、「地下鉄」、「自転車」の 4 つの移動モードを表すために使用されます。

次に、「OK」ボタンの Click イベントで、どの RadioButton コントロールが選択されているかを確認して、ユーザーがどの移動モードを選択したかを判断します。最後に、結果がメッセージボックスに表示されます。

完全なフォーム設計コードは次のとおりです。

namespace RadioButtonDemo
{
    
    
    partial class Form1
    {
    
    
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
    
    
            if (disposing && (components != null))
            {
    
    
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
    
    
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.radioButton4 = new System.Windows.Forms.RadioButton();
            this.radioButton3 = new System.Windows.Forms.RadioButton();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.button1 = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.radioButton4);
            this.groupBox1.Controls.Add(this.radioButton3);
            this.groupBox1.Controls.Add(this.radioButton2);
            this.groupBox1.Controls.Add(this.radioButton1);
            this.groupBox1.Location = new System.Drawing.Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(200, 140);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "请选择出行方式:";
            // 
            // radioButton4
            // 
            this.radioButton4.AutoSize = true;
            this.radioButton4.Location = new System.Drawing.Point(20, 105);
            this.radioButton4.Name = "radioButton4";
            this.radioButton4.Size = new System.Drawing.Size(71, 16);
            this.radioButton4.TabIndex = 3;
            this.radioButton4.Text = "自行车";
            this.radioButton4.UseVisualStyleBackColor = true;
            // 
            // radioButton3
            // 
            this.radioButton3.AutoSize = true;
            this.radioButton3.Location = new System.Drawing.Point(20, 78);
            this.radioButton3.Name = "radioButton3";
            this.radioButton3.Size = new System.Drawing.Size(59, 16);
            this.radioButton3.TabIndex = 2;
            this.radioButton3.Text = "地铁";
            this.radioButton3.UseVisualStyleBackColor = true;
            // 
            // radioButton2
            // 
            this.radioButton2.AutoSize = true;
            this.radioButton2.Location = new System.Drawing.Point(20, 51);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(71, 16);
            this.radioButton2.TabIndex = 1;
            this.radioButton2.Text = "公交车";
            this.radioButton2.UseVisualStyleBackColor = true;
            // 
            // radioButton1
            // 
            this.radioButton1.AutoSize = true;
            this.radioButton1.Checked = true;
            this.radioButton1.Location = new System.Drawing.Point(20, 24);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(71, 16);
            this.radioButton1.TabIndex = 0;
            this.radioButton1.TabStop = true;
            this.radioButton1.Text = "小汽车";
            this.radioButton1.UseVisualStyleBackColor = true;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(80, 170);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "确定";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(224, 211);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.groupBox1);
            this.Name = "Form1";
            this.Text = "RadioButtonDemo";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.RadioButton radioButton4;
        private System.Windows.Forms.RadioButton radioButton3;
        private System.Windows.Forms.RadioButton radioButton2;
        private System.Windows.Forms.RadioButton radioButton1;
        private System.Windows.Forms.Button button1;
    }
}

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/aa2528877987/article/details/132634333