在Windows窗体中,RadioButton控件用于在选项组中选择一个选项。例如,从给定列表中选择您的性别,因此您将仅在“男”或“女”或“跨性别”之类的三个选项中选择一个。在C#中,RadioButton是一个类,并且在System.Windows.Forms命名空间下定义。在RadioButton中,允许您显示文本,图像或同时显示这两者,并且当您选择一个组中的一个单选按钮时,其他单选按钮会自动清除。您可以通过两种不同的方式创建RadioButton:
1.设计时:这是创建RadioButton控件的最简单方法,如以下步骤所示:
- 第1步:创建一个Windows窗体,如下图所示:
Visual Studio->文件->新建->项目-> WindowsFormApp - 步骤2:从工具箱中拖动RadioButton控件,并将其放在Windows窗体上。您可以根据需要将RadioButton控件放置在Windows窗体上的任何位置。
- 步骤3:拖放之后,您将转到RadioButton控件的属性,以根据您的要求修改RadioButton控件。
输出:
2.运行时:比上述方法有些棘手。在此方法中,您可以在RadioButton类的帮助下以编程方式创建RadioButton。以下步骤显示了如何动态创建RadioButton:
- 步骤1:使用RadioButton类提供的RadioButton()构造函数创建单选按钮。
// Creating radio button RadioButton r1 = new RadioButton();
- 步骤2:创建RadioButton后,设置RadioButton类提供的RadioButton的属性。
// Set the AutoSize property r1.AutoSize = true; // Add text in RadioButton r1.Text = "Intern"; // Set the location of the RadioButton r1.Location = new Point(286, 40); // Set Font property r1.Font = new Font("Berlin Sans FB", 12);
- 步骤3:最后使用Add()方法将此RadioButton控件添加到表单中。
// Add this radio button to the form this.Controls.Add(r1);
例子:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp24 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void RadioButton2_CheckedChanged(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { // Creating and setting label Label l = new Label(); l.AutoSize = true; l.Location = new Point(176, 40); l.Text = "Select Post:"; l.Font = new Font("Berlin Sans FB", 12); // Adding this label to the form this.Controls.Add(l); // Creating and setting the // properties of the RadioButton RadioButton r1 = new RadioButton(); r1.AutoSize = true; r1.Text = "Intern"; r1.Location = new Point(286, 40); r1.Font = new Font("Berlin Sans FB", 12); // Adding this label to the form this.Controls.Add(r1); // Creating and setting the // properties of the RadioButton RadioButton r2 = new RadioButton(); r2.AutoSize = true; r2.Text = "Team Leader"; r2.Location = new Point(356, 40); r2.Font = new Font("Berlin Sans FB", 12); // Adding this label to the form this.Controls.Add(r2); // Creating and setting the // properties of the RadioButton RadioButton r3 = new RadioButton(); r3.AutoSize = true; r3.Text = "Software Engineer"; r3.Location = new Point(480, 40); r3.Font = new Font("Berlin Sans FB", 12); // Adding this label to the form this.Controls.Add(r3); } } }
输出:
重要属性
Property | Description |
---|---|
Appearance | This property is used to set a value determining the appearance of the RadioButton. |
AutoCheck | This property is used to set a value indicating whether the Checked value and the appearance of the RadioButton control automatically change when the RadioButton control is clicked. |
AutoSize | This property is used to set a value that indicates whether the RadioButton control resizes based on its contents. |
BackColor | This property is used to set the background color of the RadioButton control. |
CheckAlign | This property is used to set the location of the check box portion of the RadioButton. |
Checked | This property is used to set a value indicating whether the RadioButton control is checked. |
Font | This property is used to set the font of the text displayed by the RadioButton control. |
ForeColor | This property is used to set the foreground color of the RadioButton control. |
Location | This property is used to sets the coordinates of the upper-left corner of the RadioButton control relative to the upper-left corner of its form. |
Name | This property is used to sets the name of the RadioButton control. |
Padding | This property is used to sets padding within the RadioButton control. |
Text | This property is used to set the text associated with this RadioButton control. |
TextAlign | This property is used to set the alignment of the text on the RadioButton control. |
Visible | This property is used to set a value indicating whether the RadioButton control and all its child controls are displayed. |
重要事件
Event | Description |
---|---|
Click | This event occurs when the RadioButton control is clicked. |
CheckedChanged | This event occurs when the value of the Checked property changes. |
AppearanceChanged | This event occurs when the Appearance property value changes. |
DoubleClick | This event occurs when the user double-clicks the RadioButton control. |
Leave | This event occurs when the input focus leaves the RadioButton control. |
MouseClick | This event occurs when the RadioButton control is clicked by the mouse. |
MouseDoubleClick | This event occurs when the user double-clicks the RadioButton control with the mouse. |
MouseHover | This event occurs when the mouse pointer rests on the RadioButton control. |
MouseLeave | This event occurs when the mouse pointer leaves the RadioButton control. |