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 控件(如 a Form)构成一个组。 若要在一个窗体上创建多个组,请将每个组放在其自己的容器中,例如或GroupBoxPanel控件。
RadioButton 控件 CheckBox 具有类似的功能:他们提供用户可以选择或清除的选项。 区别在于可以同时选择多个 CheckBox 控件,但选项按钮是相互排斥的。
使用属性 Checked 获取或设置 . RadioButton的状态。 可以通过设置 Appearance 属性将选项按钮的外观更改为显示为切换样式按钮或标准选项按钮。
构造函数
| 名称 | 说明 |
|---|---|
| RadioButton() |
初始化 RadioButton 类的新实例。 |
属性
| 名称 | 说明 |
|---|---|
| AccessibilityObject |
AccessibleObject获取分配给控件的控件。 (继承自 Control) |
| AccessibleDefaultActionDescription |
获取或设置控件的默认操作说明,以供辅助功能客户端应用程序使用。 (继承自 Control) |
| AccessibleDescription |
获取或设置辅助功能客户端应用程序使用的控件的说明。 (继承自 Control) |
| AccessibleName |
获取或设置辅助功能客户端应用程序使用的控件的名称。 (继承自 Control) |
| AccessibleRole |
获取或设置控件的可访问角色。 (继承自 Control) |
| AllowDrop |
获取或设置一个值,该值指示控件是否可以接受用户拖动到其中的数据。 (继承自 Control) |
| Anchor |
获取或设置控件绑定到的容器的边缘,并确定控件的父级如何调整其大小。 (继承自 Control) |
| Appearance |
获取或设置一个值,该值确定 . RadioButton的外观。 |
| AutoCheck |
获取或设置一个值,该值指示单击控件时控件的值和外观是否 Checked 自动更改。 |
| AutoEllipsis |
获取或设置一个值,该值指示省略号字符 (...) 是否出现在控件的右边缘,表示控件文本超出控件的指定长度。 (继承自 ButtonBase) |
| AutoScrollOffset |
获取或设置此控件滚动到的位置 ScrollControlIntoView(Control)。 (继承自 Control) |
| AutoSize |
获取或设置一个值,该值指示控件是否根据其内容调整大小。 (继承自 ButtonBase) |
| BackColor |
获取或设置控件的背景色。 (继承自 ButtonBase) |
| BackgroundImage |
获取或设置控件中显示的背景图像。 (继承自 Control) |
| BackgroundImageLayout |
获取或设置枚举中 ImageLayout 定义的背景图像布局。 (继承自 Control) |
| BindingContext |
获取或设置 BindingContext 控件。 (继承自 Control) |
| Bottom |
获取控件的下边缘与其容器工作区的上边缘之间的距离(以像素为单位)。 (继承自 Control) |
| Bounds |
获取或设置控件的大小和位置,包括其相对于父控件的非client 元素(以像素为单位)。 (继承自 Control) |
| CanEnableIme |
获取一个值,该值指示属性是否可以 ImeMode 设置为活动值,以启用 IME 支持。 (继承自 Control) |
| CanFocus |
获取一个值,该值指示控件是否可以接收焦点。 (继承自 Control) |
| CanRaiseEvents |
确定是否可以在控件上引发事件。 (继承自 Control) |
| CanSelect |
获取一个值,该值指示是否可以选择控件。 (继承自 Control) |
| Capture |
获取或设置一个值,该值指示控件是否已捕获鼠标。 (继承自 Control) |
| CausesValidation |
获取或设置一个值,该值指示控件是否导致验证在收到焦点时需要验证的任何控件上执行。 (继承自 Control) |
| CheckAlign |
获取或设置复选框部分 RadioButton的位置。 |
| Checked |
获取或设置一个值,该值指示是否检查控件。 |
| ClientRectangle |
获取表示控件工作区的矩形。 (继承自 Control) |
| ClientSize |
获取或设置控件工作区的高度和宽度。 (继承自 Control) |
| Command |
获取或设置ICommand调用事件时Click将调用其Execute(Object)方法。 (继承自 ButtonBase) |
| CommandParameter |
获取或设置传递给 ICommand 分配给属性的参数 Command 。 (继承自 ButtonBase) |
| CompanyName |
获取包含控件的应用程序的公司或创建者的名称。 (继承自 Control) |
| Container |
IContainer获取包含 .Component (继承自 Component) |
| ContainsFocus |
获取一个值,该值指示控件或其子控件之一当前是否具有输入焦点。 (继承自 Control) |
| ContextMenu |
已过时.
获取或设置与控件关联的快捷菜单。 (继承自 Control) |
| ContextMenuStrip |
获取或设置 ContextMenuStrip 与此控件关联的值。 (继承自 Control) |
| Controls |
获取控件中包含的控件的集合。 (继承自 Control) |
| Created |
获取一个值,该值指示是否已创建控件。 (继承自 Control) |
| CreateParams |
获取创建控件句柄时所需的创建参数。 |
| Cursor |
获取或设置鼠标指针位于控件上时显示的光标。 (继承自 Control) |
| DataBindings |
获取控件的数据绑定。 (继承自 Control) |
| DataContext |
获取或设置用于数据绑定的数据上下文。 这是一个环境属性。 (继承自 Control) |
| DefaultCursor |
获取或设置控件的默认游标。 (继承自 Control) |
| DefaultImeMode |
获取此控件支持的默认输入法编辑器 (IME) 模式。 (继承自 ButtonBase) |
| DefaultMargin |
获取默认情况下在控件之间指定的空间(以像素为单位)。 (继承自 Control) |
| DefaultMaximumSize |
获取指定为控件的默认最大大小的长度和高度(以像素为单位)。 (继承自 Control) |
| DefaultMinimumSize |
获取指定为控件的默认最小大小的长度和高度(以像素为单位)。 (继承自 Control) |
| DefaultPadding |
获取控件内容的默认内部间距(以像素为单位)。 (继承自 Control) |
| DefaultSize |
获取控件的默认大小。 |
| DesignMode |
获取一个值,该值指示当前是否 Component 处于设计模式。 (继承自 Component) |
| DeviceDpi |
获取当前显示控件的显示设备的 DPI 值。 (继承自 Control) |
| DisplayRectangle |
获取表示控件的显示区域的矩形。 (继承自 Control) |
| Disposing |
获取一个值,该值指示基 Control 类是否正在处理。 (继承自 Control) |
| Dock |
获取或设置哪些控件边框停靠到其父控件,并确定控件如何调整其父级的大小。 (继承自 Control) |
| DoubleBuffered |
获取或设置一个值,该值指示此控件是否应使用辅助缓冲区重新绘制其表面以减少或防止闪烁。 (继承自 Control) |
| Enabled |
获取或设置一个值,该值指示控件是否可以响应用户交互。 (继承自 Control) |
| Events |
获取附加到此 Component对象的事件处理程序的列表。 (继承自 Component) |
| FlatAppearance |
获取边框的外观以及用于指示检查状态和鼠标状态的颜色。 (继承自 ButtonBase) |
| FlatStyle |
获取或设置按钮控件的平面样式外观。 (继承自 ButtonBase) |
| Focused |
获取一个值,该值指示控件是否具有输入焦点。 (继承自 Control) |
| Font |
获取或设置控件显示的文本的字体。 (继承自 Control) |
| FontHeight |
获取或设置控件字体的高度。 (继承自 Control) |
| ForeColor |
获取或设置控件的前景色。 (继承自 Control) |
| Handle |
获取控件绑定到的窗口句柄。 (继承自 Control) |
| HasChildren |
获取一个值,该值指示控件是否包含一个或多个子控件。 (继承自 Control) |
| Height |
获取或设置控件的高度。 (继承自 Control) |
| Image |
获取或设置按钮控件上显示的图像。 (继承自 ButtonBase) |
| ImageAlign |
获取或设置按钮控件上图像的对齐方式。 (继承自 ButtonBase) |
| ImageIndex |
获取或设置按钮控件上显示的图像的图像列表索引值。 (继承自 ButtonBase) |
| ImageKey |
获取或设置图像的 ImageList键访问器。 (继承自 ButtonBase) |
| ImageList |
获取或设置 ImageList 包含 Image 按钮控件上显示的内容。 (继承自 ButtonBase) |
| ImeMode |
获取或设置此控件支持的输入法编辑器 (IME) 模式。 此属性与此类无关。 (继承自 ButtonBase) |
| ImeModeBase |
获取或设置控件的 IME 模式。 (继承自 Control) |
| InvokeRequired |
获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用调用方法,因为调用方与创建控件的线程不同。 (继承自 Control) |
| IsAccessible |
获取或设置一个值,该值指示控件是否对辅助功能应用程序可见。 (继承自 Control) |
| IsAncestorSiteInDesignMode |
指示此控件的上级位置之一是否位于 DesignMode 中。 此属性为只读。 (继承自 Control) |
| IsDefault |
获取或设置一个值,该值指示按钮控件是否为默认按钮。 (继承自 ButtonBase) |
| IsDisposed |
获取一个值,该值指示控件是否已释放。 (继承自 Control) |
| IsHandleCreated |
获取一个值,该值指示控件是否具有与之关联的句柄。 (继承自 Control) |
| IsMirrored |
获取一个值,该值指示控件是否镜像。 (继承自 Control) |
| LayoutEngine |
获取控件布局引擎的缓存实例。 (继承自 Control) |
| Left |
获取或设置控件左边缘与其容器工作区的左边缘之间的距离(以像素为单位)。 (继承自 Control) |
| Location |
获取或设置控件左上角相对于其容器左上角的坐标。 (继承自 Control) |
| Margin |
获取或设置控件之间的间距。 (继承自 Control) |
| MaximumSize |
获取或设置可指定上限 GetPreferredSize(Size) 的大小。 (继承自 Control) |
| MinimumSize |
获取或设置可以指定的下限 GetPreferredSize(Size) 的大小。 (继承自 Control) |
| Name |
获取或设置控件的名称。 (继承自 Control) |
| Padding |
获取或设置控件中的填充。 (继承自 Control) |
| Parent |
获取或设置控件的父容器。 (继承自 Control) |
| PreferredSize |
获取控件可以容纳到的矩形区域的大小。 (继承自 Control) |
| ProductName |
获取包含控件的程序集的产品名称。 (继承自 Control) |
| ProductVersion |
获取包含控件的程序集的版本。 (继承自 Control) |
| RecreatingHandle |
获取一个值,该值指示控件当前是否正在重新创建其句柄。 (继承自 Control) |
| Region |
获取或设置与控件关联的窗口区域。 (继承自 Control) |
| RenderRightToLeft |
已过时.
已过时.
此属性现已过时。 (继承自 Control) |
| ResizeRedraw |
获取或设置一个值,该值指示控件在调整大小时是否重新绘制自身。 (继承自 Control) |
| Right |
获取控件右边缘与其容器工作区的左边缘之间的距离(以像素为单位)。 (继承自 Control) |
| RightToLeft |
获取或设置一个值,该值指示控件的元素是否对齐以支持使用从右到左字体的区域设置。 (继承自 Control) |
| ScaleChildren |
获取一个值,该值确定子控件的缩放。 (继承自 Control) |
| ShowFocusCues |
获取一个值,该值指示控件是否应显示焦点矩形。 (继承自 Control) |
| ShowKeyboardCues |
获取一个值,该值指示用户界面是否处于适当的状态以显示或隐藏键盘加速器。 (继承自 Control) |
| Site |
获取或设置控件的站点。 (继承自 Control) |
| Size |
获取或设置控件的高度和宽度。 (继承自 Control) |
| TabIndex |
获取或设置控件在其容器中的 Tab 键顺序。 (继承自 Control) |
| TabStop |
获取或设置一个值,该值指示用户是否可以使用 TAB 键向此控件提供焦点。 |
| Tag |
获取或设置包含有关控件的数据的对象。 (继承自 Control) |
| Text |
获取或设置与此控件关联的文本。 (继承自 ButtonBase) |
| TextAlign |
获取或设置控件上文本的 RadioButton 对齐方式。 |
| TextImageRelation |
获取或设置文本和图像相对于彼此的位置。 (继承自 ButtonBase) |
| Top |
获取或设置控件上边缘与其容器工作区上边缘之间的距离(以像素为单位)。 (继承自 Control) |
| TopLevelControl |
获取其他 Windows 窗体控件未父控件的父控件。 通常,这是控件包含在的最外层 Form 。 (继承自 Control) |
| UseCompatibleTextRendering |
获取或设置一个值,该值确定是否使用 Graphics 类(GDI+)或 TextRenderer 类(GDI)来呈现文本。 (继承自 ButtonBase) |
| UseMnemonic |
获取或设置一个值,该值指示第一个字符前面是否使用安和(&)作为控件的助记键。 (继承自 ButtonBase) |
| UseVisualStyleBackColor |
获取或设置一个值,该值确定是否使用视觉样式绘制背景(如果受支持)。 (继承自 ButtonBase) |
| UseWaitCursor |
获取或设置一个值,该值指示是否对当前控件和所有子控件使用等待游标。 (继承自 Control) |
| Visible |
获取或设置一个值,该值指示是否显示控件及其所有子控件。 (继承自 Control) |
| Width |
获取或设置控件的宽度。 (继承自 Control) |
| WindowTarget |
此属性与此类无关。 (继承自 Control) |
方法
活动
显式接口实现
| 名称 | 说明 |
|---|---|
| IDropTarget.OnDragDrop(DragEventArgs) |
引发 DragDrop 事件。 (继承自 Control) |
| IDropTarget.OnDragEnter(DragEventArgs) |
引发 DragEnter 事件。 (继承自 Control) |
| IDropTarget.OnDragLeave(EventArgs) |
引发 DragLeave 事件。 (继承自 Control) |
| IDropTarget.OnDragOver(DragEventArgs) |
引发 DragOver 事件。 (继承自 Control) |