[Yugong Series] Detailed Explanation of RadioButton Controls on Winform Controls in September 2023


foreword

Winform controls are user interface elements in Windows Forms. They can be used to create various visual and interactive components of Windows applications, such as buttons, labels, text boxes, drop-down list boxes, check boxes, radio boxes, progress bars, etc. . Developers can use Winform controls to build user interfaces and respond to user actions to create powerful desktop applications.

1. Detailed explanation of RadioButton control

The RadioButton control is a commonly used control in Winform, which is used to allow the user to select an option in a set of mutually exclusive options.

Following are the steps to use RadioButton control in Winform:

  1. Open Visual Studio, create a new project, select Windows Forms Application.

  2. In Design view, drag a RadioButton control from the Toolbox onto the form.

  3. You can use the property window to set its text, position, size and other properties.

  4. You can see the Name property in the property window, you can give the RadioButton control a meaningful name to facilitate referencing it in the code.

  5. RadioButton controls in the same group of options should be wrapped with the same container control (such as Panel or GroupBox).

  6. In code, you can use the Checked property to determine whether the RadioButton control is selected. like:

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

Precautions:

  1. RadioButton controls must be used in conjunction with other RadioButton controls in the same group, otherwise mutual exclusion cannot be achieved.

  2. The CheckedChanged event of the RadioButton controls in the same group can perform selection judgment and operation within the group.

  3. The RadioButton control can also set the selected state through code, such as:

radioButton1.Checked = true;

The above is how to use the RadioButton control in Winform.

1. Attribute introduction

1.1 CheckAlign

The RadioButton control is one of the commonly used controls in Winform, which is used to provide radio selection function. The CheckAlign property is used to set the selected circle position in the RadioButton control.

The CheckAlign property has the following three enumeration values:

  1. TopLeft: The selected circle is in the upper left corner of the RadioButton control.

  2. MiddleLeft: The selected circle is in the middle of the RadioButton control, to the left.

  3. BottomLeft: The selected circle is at the bottom of the RadioButton control, to the left.

Using the CheckAlign property requires setting it at design time or in code. When designing, select the RadioButton control, then find the CheckAlign property in the property panel, and set the corresponding enumeration value through the drop-down menu. In code, this can be achieved by setting the CheckAlign property of the RadioButton control.

Here is a simple sample code:

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

The above code creates a RadioButton control, and sets its text to "Option 1", and sets the position of the selected circle to the middle position, left.

2. Common scenarios

RadioButton controls are commonly used in the following scenarios in Winform:

  1. Users need to select from multiple options, and the RadioButton control can be used to present these options.
  2. Using the RadioButton control in the form allows users to select personal information such as gender and marital status.
  3. On a settings page in an application, a RadioButton control can be used to allow the user to select an item from options to change the application's settings.
  4. In a survey application, a RadioButton control can be used to let the user select an answer from multiple options.
  5. In a game, a RadioButton control can be used to allow the player to select the difficulty level of the game.

3. Specific cases

The following is a complete Winform application that demonstrates how to use the RadioButton control to allow users to choose their own travel mode and display the selection results:

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);
        }
    }
}

In this application, we first add four RadioButton controls to the form, which are used to represent the four travel modes of "car", "bus", "subway" and "bicycle".

Then, in the Click event of the "OK" button, determine which travel mode the user has selected by checking which RadioButton control is selected. Finally, the result is displayed in the MessageBox.

The complete form design code is as follows:

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;
    }
}

insert image description here

Guess you like

Origin blog.csdn.net/aa2528877987/article/details/132634333