CheckBox.CheckState 属性

定义

获取或设置 CheckBox 的状态。

public:
 property System::Windows::Forms::CheckState CheckState { System::Windows::Forms::CheckState get(); void set(System::Windows::Forms::CheckState value); };
[System.ComponentModel.Bindable(true)]
public System.Windows.Forms.CheckState CheckState { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.CheckState : System.Windows.Forms.CheckState with get, set
Public Property CheckState As CheckState

属性值

CheckState 枚举值之一。 默认值是 Unchecked

属性

例外

所分配的值不是 CheckState 枚举值之一。

示例

下面的代码示例显示标签中三个属性的值。 属性ThreeState在 和 false 之间true交替单击控件CheckAlign,在 和 MiddleLeftSystem.Drawing.ContentAlignment之间MiddleRight交替单击。 此示例演示属性值如何随着 ThreeState 属性更改和控件的检查而更改。 此示例要求 CheckBox在窗体上实例化 和 ButtonLabel ,并且标签足够大,以显示三行文本,以及对 命名空间的System.Drawing引用。 应在 控件的 Click 事件处理程序中调用此代码。

private:
   void AdjustMyCheckBoxProperties()
   {
      // Concatenate the property values together on three lines.
      label1->Text = String::Format( "ThreeState: {0}\nChecked: {1}\nCheckState: {2}",
         checkBox1->ThreeState, checkBox1->Checked, checkBox1->CheckState );
      
      // Change the ThreeState and CheckAlign properties on every other click.
      if ( !checkBox1->ThreeState )
      {
         checkBox1->ThreeState = true;
         checkBox1->CheckAlign = ContentAlignment::MiddleRight;
      }
      else
      {
         checkBox1->ThreeState = false;
         checkBox1->CheckAlign = ContentAlignment::MiddleLeft;
      }
   }
private void AdjustMyCheckBoxProperties()
 {
    // Change the ThreeState and CheckAlign properties on every other click.
    if (!checkBox1.ThreeState)
    {
       checkBox1.ThreeState = true;
       checkBox1.CheckAlign = ContentAlignment.MiddleRight;
    }
    else
    {
       checkBox1.ThreeState = false;
       checkBox1.CheckAlign = ContentAlignment.MiddleLeft;
    }

    // Concatenate the property values together on three lines.
    label1.Text = "ThreeState: " + checkBox1.ThreeState.ToString() + "\n" +
                  "Checked: " + checkBox1.Checked.ToString() + "\n" +
                  "CheckState: " + checkBox1.CheckState.ToString(); 
 }
Private Sub AdjustMyCheckBoxProperties()

    ' Change the ThreeState and CheckAlign properties on every other click.
    If Not checkBox1.ThreeState Then
        checkBox1.ThreeState = True
        checkBox1.CheckAlign = ContentAlignment.MiddleRight
    Else
        checkBox1.ThreeState = False
        checkBox1.CheckAlign = ContentAlignment.MiddleLeft
    End If

    ' Concatenate the property values together on three lines.
    label1.Text = "ThreeState: " & checkBox1.ThreeState.ToString() & ControlChars.Cr & _
        "Checked: " & checkBox1.Checked.ToString() & ControlChars.Cr & _
        "CheckState: " & checkBox1.CheckState.ToString()

End Sub

注解

如果 属性 ThreeState 设置为 false,则 CheckState 只能在代码中将属性值设置为 CheckState.Indeterminate ,而不能通过用户交互。

下表描述了 System.Windows.Forms.Appearance 和 样式控件 处于不同状态的 控件CheckBox.AppearanceNormalButtonCheckBox

CheckState Appearance.Normal Appearance.Button
Checked 显示CheckBox检查标记。 控件显示为已沉没。
Unchecked CheckBox 为空。 控件将显示为凸起。
Indeterminate 显示CheckBox检查标记,并带有阴影。 该控件以平面显示。

适用于

另请参阅