RadioButton 类
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当与其他 RadioButton 控件成对出现时,使用户能够从一组选项中选择一个选项。
public ref class RadioButton : System::Windows::Forms::ButtonBase
public class RadioButton : System.Windows.Forms.ButtonBase
[System.ComponentModel.DefaultBindingProperty("Checked")]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
public class RadioButton : System.Windows.Forms.ButtonBase
[System.ComponentModel.DefaultBindingProperty("Checked")]
public class RadioButton : System.Windows.Forms.ButtonBase
type RadioButton = class
inherit ButtonBase
[<System.ComponentModel.DefaultBindingProperty("Checked")>]
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type RadioButton = class
inherit ButtonBase
[<System.ComponentModel.DefaultBindingProperty("Checked")>]
type RadioButton = class
inherit ButtonBase
Public Class RadioButton
Inherits ButtonBase
- 继承
- 属性
下面的代码示例创建并初始化 中的两 RadioButton 个 GroupBox控件。 该示例使用 CheckedChanged 事件来跟踪RadioButton所选事件,并在用户单击 Button时报告所选 RadioButton 的文本。 若要运行此示例,请将代码粘贴到 Windows 窗体中。 从窗体的构造函数或Load事件处理方法调用 InitializeRadioButtons
。
private GroupBox groupBox1;
private RadioButton radioButton2;
private RadioButton radioButton1;
private RadioButton selectedrb;
private Button getSelectedRB;
public void InitializeRadioButtons()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.getSelectedRB = new System.Windows.Forms.Button();
this.groupBox1.Controls.Add(this.radioButton2);
this.groupBox1.Controls.Add(this.radioButton1);
this.groupBox1.Controls.Add(this.getSelectedRB);
this.groupBox1.Location = new System.Drawing.Point(30, 100);
this.groupBox1.Size = new System.Drawing.Size(220, 125);
this.groupBox1.Text = "Radio Buttons";
this.radioButton2.Location = new System.Drawing.Point(31, 53);
this.radioButton2.Size = new System.Drawing.Size(67, 17);
this.radioButton2.Text = "Choice 2";
this.radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
this.radioButton1.Location = new System.Drawing.Point(31, 20);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(67, 17);
this.radioButton1.Text = "Choice 1";
this.radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
this.getSelectedRB.Location = new System.Drawing.Point(10, 75);
this.getSelectedRB.Size = new System.Drawing.Size(200, 25);
this.getSelectedRB.Text = "Get selected RadioButton";
this.getSelectedRB.Click += new EventHandler(getSelectedRB_Click);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.groupBox1);
}
void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
if (rb == null)
{
MessageBox.Show("Sender is not a RadioButton");
return;
}
// Ensure that the RadioButton.Checked property
// changed to true.
if (rb.Checked)
{
// Keep track of the selected RadioButton by saving a reference
// to it.
selectedrb = rb;
}
}
// Show the text of the selected RadioButton.
void getSelectedRB_Click(object sender, EventArgs e)
{
MessageBox.Show(selectedrb.Text);
}
Private groupBox1 As GroupBox
Private radioButton2 As RadioButton
Private radioButton1 As RadioButton
Private selectedrb As RadioButton
Private WithEvents getSelectedRB As Button
Public Sub InitializeRadioButtons()
Me.groupBox1 = New System.Windows.Forms.GroupBox()
Me.radioButton2 = New System.Windows.Forms.RadioButton()
Me.radioButton1 = New System.Windows.Forms.RadioButton()
Me.getSelectedRB = New System.Windows.Forms.Button()
Me.groupBox1.Controls.Add(Me.radioButton2)
Me.groupBox1.Controls.Add(Me.radioButton1)
Me.groupBox1.Controls.Add(Me.getSelectedRB)
Me.groupBox1.Location = New System.Drawing.Point(30, 100)
Me.groupBox1.Size = New System.Drawing.Size(220, 125)
Me.groupBox1.Text = "Radio Buttons"
Me.radioButton2.Location = New System.Drawing.Point(31, 53)
Me.radioButton2.Size = New System.Drawing.Size(67, 17)
Me.radioButton2.Text = "Choice 2"
AddHandler Me.radioButton2.Click, AddressOf radioButton_CheckedChanged
Me.radioButton1.Location = New System.Drawing.Point(31, 20)
Me.radioButton1.Name = "radioButton1"
Me.radioButton1.Size = New System.Drawing.Size(67, 17)
Me.radioButton1.Text = "Choice 1"
AddHandler Me.radioButton1.Click, AddressOf radioButton_CheckedChanged
Me.getSelectedRB.Location = New System.Drawing.Point(10, 75)
Me.getSelectedRB.Size = New System.Drawing.Size(200, 25)
Me.getSelectedRB.Text = "Get selected RadioButton"
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.groupBox1)
End Sub
Private Sub radioButton_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) _
Dim rb As RadioButton = TryCast(sender, RadioButton)
If rb Is Nothing Then
MessageBox.Show("Sender is not a RadioButton")
Return
End If
' Ensure that the RadioButton.Checked property
' changed to true.
If rb.Checked Then
' Keep track of the selected RadioButton by saving a reference
' to it.
selectedrb = rb
End If
End Sub
' Show the text of the selected RadioButton.
Private Sub getSelectedRB_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles getSelectedRB.Click
MessageBox.Show(selectedrb.Text)
End Sub
控件 RadioButton 可以显示文本和 Image/或 。
当用户在组中选择一个选项按钮 (也称为单选按钮) 时,其他按钮会自动清除。 RadioButton给定容器中的所有控件(如 Form)构成一个组。 若要在一个窗体上创建多个组,请将每个组放在其自己的容器中,例如 GroupBox 或 Panel 控件。
RadioButton 和 CheckBox 控件具有类似的功能:它们提供用户可以选择或清除的选项。 不同之处在于,可以同时选择多个 CheckBox 控件,但选项按钮是互斥的。
Checked使用 属性获取或设置 的状态RadioButton。 可以通过设置 Appearance 属性更改选项按钮的外观,以显示为切换样式按钮或标准选项按钮。
Radio |
初始化 RadioButton 类的新实例。 |
Accessibility |
获取分配给该控件的 AccessibleObject。 (继承自 Control) |
Accessible |
获取或设置控件的默认操作说明以供具有辅助功能的客户端应用程序使用。 (继承自 Control) |
Accessible |
获取或设置辅助功能客户端应用程序使用的控件说明。 (继承自 Control) |
Accessible |
获取或设置辅助功能客户端应用程序所使用的控件名称。 (继承自 Control) |
Accessible |
获取或设置控件的辅助性角色。 (继承自 Control) |
Allow |
获取或设置一个值,该值指示控件是否可以接受用户拖放到它上面的数据。 (继承自 Control) |
Anchor |
获取或设置控件绑定到的容器的边缘并确定控件如何随其父级一起调整大小。 (继承自 Control) |
Appearance |
获取或设置一个值,该值用于确定 RadioButton 的外观。 |
Auto |
获取或设置一个值,它指示:在单击控件时,Checked 值和控件的外观是否自动更改。 |
Auto |
获取或设置一个值,该值指示是否要在控件的右边缘显示省略号 (...) 以表示控件文本超出指定的控件长度。 (继承自 ButtonBase) |
Auto |
获取或设置一个值,该值指示在 ScrollControlIntoView(Control) 中将控件滚动到何处。 (继承自 Control) |
Auto |
获取或设置一个值,该值指示控件是否基于其内容调整大小。 (继承自 ButtonBase) |
Back |
获取或设置控件的背景色。 (继承自 ButtonBase) |
Background |
获取或设置在控件中显示的背景图像。 (继承自 Control) |
Background |
获取或设置在 ImageLayout 枚举中定义的背景图像布局。 (继承自 Control) |
Binding |
获取或设置控件的 BindingContext。 (继承自 Control) |
Bottom |
获取控件下边缘与其容器的工作区上边缘之间的距离(以像素为单位)。 (继承自 Control) |
Bounds |
获取或设置控件(包括其非工作区元素)相对于其父控件的大小和位置(以像素为单位)。 (继承自 Control) |
Can |
获取一个用以指示是否可以将 ImeMode 属性设置为活动值的值,以启用 IME 支持。 (继承自 Control) |
Can |
获取一个值,该值指示控件是否可以接收焦点。 (继承自 Control) |
Can |
确定是否可以在控件上引发事件。 (继承自 Control) |
Can |
获取一个值,该值指示是否可以选中控件。 (继承自 Control) |
Capture |
获取或设置一个值,该值指示控件是否已捕获鼠标。 (继承自 Control) |
Causes |
获取或设置一个值,该值指示控件是否会引起在任何需要在接收焦点时执行验证的控件上执行验证。 (继承自 Control) |
Check |
获取或设置 RadioButton 的复选框部分的位置。 |
Checked |
获取或设置一个值,该值指示是否已选中控件。 |
Client |
获取表示控件的工作区的矩形。 (继承自 Control) |
Client |
获取或设置控件的工作区的高度和宽度。 (继承自 Control) |
Command |
获取或设置在ICommand调用 事件时Click将调用其Execute(Object)方法的 。 (继承自 ButtonBase) |
Command |
获取或设置传递给 ICommand 分配给 Command 属性的 的参数。 (继承自 ButtonBase) |
Company |
获取包含控件的应用程序的公司名称或创建者。 (继承自 Control) |
Container |
获取包含 IContainer 的 Component。 (继承自 Component) |
Contains |
获取一个值,该值指示控件或它的一个子控件当前是否有输入焦点。 (继承自 Control) |
Context |
获取或设置与控件关联的快捷菜单。 (继承自 Control) |
Context |
获取或设置与此控件关联的 ContextMenuStrip。 (继承自 Control) |
Controls |
获取包含在控件内的控件的集合。 (继承自 Control) |
Created |
获取一个值,该值指示控件是否已经创建。 (继承自 Control) |
Create |
获取创建控件句柄时所需要的创建参数。 |
Cursor |
获取或设置当鼠标指针位于控件上时显示的光标。 (继承自 Control) |
Data |
为该控件获取数据绑定。 (继承自 Control) |
Data |
获取或设置用于数据绑定的数据上下文。 这是一个环境属性。 (继承自 Control) |
Default |
获取或设置控件的默认光标。 (继承自 Control) |
Default |
获取此控件支持的默认输入法编辑器 (IME) 模式。 (继承自 ButtonBase) |
Default |
获取控件之间默认指定的间距(以像素为单位)。 (继承自 Control) |
Default |
获取以像素为单位的长度和高度,此长度和高度被指定为控件的默认最大大小。 (继承自 Control) |
Default |
获取以像素为单位的长度和高度,此长度和高度被指定为控件的默认最小大小。 (继承自 Control) |
Default |
获取控件内容的默认内部间距(以像素为单位)。 (继承自 Control) |
Default |
获取控件的默认大小。 |
Design |
获取一个值,用以指示 Component 当前是否处于设计模式。 (继承自 Component) |
Device |
获取显示当前控件的显示设备的 DPI 值。 (继承自 Control) |
Display |
获取表示控件的显示区域的矩形。 (继承自 Control) |
Disposing |
获取一个值,该值指示 Control 基类是否在释放进程中。 (继承自 Control) |
Dock |
获取或设置哪些控件边框停靠到其父控件并确定控件如何随其父级一起调整大小。 (继承自 Control) |
Double |
获取或设置一个值,该值指示此控件是否应使用辅助缓冲区重绘其图面,以减少或避免闪烁。 (继承自 Control) |
Enabled |
获取或设置一个值,该值指示控件是否可以对用户交互作出响应。 (继承自 Control) |
Events |
获取附加到此 Component 的事件处理程序的列表。 (继承自 Component) |
Flat |
获取用于指示选中状态和鼠标状态的边框外观和颜色。 (继承自 ButtonBase) |
Flat |
获取或设置按钮控件的平面样式外观。 (继承自 ButtonBase) |
Focused |
获取一个值,该值指示控件是否有输入焦点。 (继承自 Control) |
Font |
获取或设置控件显示的文字的字体。 (继承自 Control) |
Font |
获取或设置控件的字体的高度。 (继承自 Control) |
Fore |
获取或设置控件的前景色。 (继承自 Control) |
Handle |
获取控件绑定到的窗口句柄。 (继承自 Control) |
Has |
获取一个值,该值指示控件是否包含一个或多个子控件。 (继承自 Control) |
Height |
获取或设置控件的高度。 (继承自 Control) |
Image |
获取或设置显示在按钮控件上的图像。 (继承自 ButtonBase) |
Image |
获取或设置按钮控件上的图像对齐方式。 (继承自 ButtonBase) |
Image |
获取或设置按钮控件上显示的图像的图像列表索引值。 (继承自 ButtonBase) |
Image |
获取或设置 ImageList 中的图像的键访问器。 (继承自 ButtonBase) |
Image |
获取或设置包含按钮控件上显示的 ImageList 的 Image。 (继承自 ButtonBase) |
Ime |
获取或设置此控件所支持的输入法编辑器 (IME) 模式。 此属性与此类无关。 (继承自 ButtonBase) |
Ime |
获取或设置控件的 IME 模式。 (继承自 Control) |
Invoke |
获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用 Invoke 方法,因为调用方位于创建控件所在的线程以外的线程中。 (继承自 Control) |
Is |
获取或设置一个值,该值指示控件对辅助功能应用程序是否可见。 (继承自 Control) |
Is |
指示此控件的上级之一是否位于 DesignMode 中。 此属性为只读。 (继承自 Control) |
Is |
获取或设置一个值,通过该值指示按钮控件是否为默认按钮。 (继承自 ButtonBase) |
Is |
获取一个值,该值指示控件是否已经被释放。 (继承自 Control) |
Is |
获取一个值,该值指示控件是否有与它关联的句柄。 (继承自 Control) |
Is |
获取一个值,该值指示此控件是否为镜像控件。 (继承自 Control) |
Layout |
获取控件的布局引擎的缓存实例。 (继承自 Control) |
Left |
获取或设置控件左边缘与其容器的工作区左边缘之间的距离(以像素为单位)。 (继承自 Control) |
Location |
获取或设置该控件的左上角相对于其容器的左上角的坐标。 (继承自 Control) |
Margin |
获取或设置控件之间的空间。 (继承自 Control) |
Maximum |
获取或设置大小,该大小是 GetPreferredSize(Size) 可以指定的上限。 (继承自 Control) |
Minimum |
获取或设置大小,该大小是 GetPreferredSize(Size) 可以指定的下限。 (继承自 Control) |
Name |
获取或设置控件的名称。 (继承自 Control) |
Padding |
获取或设置控件内的空白。 (继承自 Control) |
Parent |
获取或设置控件的父容器。 (继承自 Control) |
Preferred |
获取可以容纳控件的矩形区域的大小。 (继承自 Control) |
Product |
获取包含控件的程序集的产品名称。 (继承自 Control) |
Product |
获取包含控件的程序集的版本。 (继承自 Control) |
Recreating |
获取一个值,该值指示控件当前是否在重新创建其句柄。 (继承自 Control) |
Region |
获取或设置与控件关联的窗口区域。 (继承自 Control) |
Render |
已过时.
已过时.
此属性现已过时。 (继承自 Control) |
Resize |
获取或设置一个值,该值指示控件在调整大小时是否重绘自己。 (继承自 Control) |
Right |
获取控件右边缘与其容器的工作区左边缘之间的距离(以像素为单位)。 (继承自 Control) |
Right |
获取或设置一个值,该值指示是否将控件的元素对齐以支持使用从右向左的字体的区域设置。 (继承自 Control) |
Scale |
获取一个值,该值确定子控件的缩放。 (继承自 Control) |
Show |
获取一个值,该值指示控件是否应显示聚焦框。 (继承自 Control) |
Show |
获取一个值,该值指示用户界面是否处于适当的状态以显示或隐藏键盘快捷键。 (继承自 Control) |
Site |
获取或设置控件的站点。 (继承自 Control) |
Size |
获取或设置控件的高度和宽度。 (继承自 Control) |
Tab |
获取或设置控件在其容器内的 Tab 键顺序。 (继承自 Control) |
Tab |
获取或设置一个值,该值指示用户能否使用 Tab 键将焦点放到该控件上。 |
Tag |
获取或设置包含有关控件的数据的对象。 (继承自 Control) |
Text |
获取或设置与此控件关联的文本。 (继承自 ButtonBase) |
Text |
获取或设置 RadioButton 控件上的文本对齐方式。 |
Text |
获取或设置文本和图像相互之间的相对位置。 (继承自 ButtonBase) |
Top |
获取或设置控件上边缘与其容器的工作区上边缘之间的距离(以像素为单位)。 (继承自 Control) |
Top |
获取没有另一个 Windows 窗体控件作为其父级的父控件。 通常,这是控件所在的最外面的 Form。 (继承自 Control) |
Use |
获取或设置一个值,该值确定是使用 Graphics 类 (GDI+) 还是 TextRenderer 使用 GDI) 类来呈现文本 (GDI。 (继承自 ButtonBase) |
Use |
获取或设置一个值,该值指示前面有“&”符的第一个字符是否用作控件的助记键。 (继承自 ButtonBase) |
Use |
获取或设置一个值,确定是否使用视觉样式(如果支持)绘制背景。 (继承自 ButtonBase) |
Use |
获取或设置一个值,该值指示是否将等待光标用于当前控件以及所有子控件。 (继承自 Control) |
Visible |
获取或设置一个值,该值指示是否显示该控件及其所有子控件。 (继承自 Control) |
Width |
获取或设置控件的宽度。 (继承自 Control) |
Window |
此属性与此类无关。 (继承自 Control) |
IDrop |
引发 DragDrop 事件。 (继承自 Control) |
IDrop |
引发 DragEnter 事件。 (继承自 Control) |
IDrop |
引发 DragLeave 事件。 (继承自 Control) |
IDrop |
引发 DragOver 事件。 (继承自 Control) |
产品 | 版本 |
---|---|
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9 |