CheckBox.CheckState 属性

获取或设置 CheckBox 的状态。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<BindableAttribute(True)> _
Public Property CheckState As CheckState
用法
Dim instance As CheckBox
Dim value As CheckState

value = instance.CheckState

instance.CheckState = value
[BindableAttribute(true)] 
public CheckState CheckState { get; set; }
[BindableAttribute(true)] 
public:
property CheckState CheckState {
    CheckState get ();
    void set (CheckState value);
}
/** @property */
public CheckState get_CheckState ()

/** @property */
public void set_CheckState (CheckState value)
public function get CheckState () : CheckState

public function set CheckState (value : CheckState)

属性值

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

异常

异常类型 条件

InvalidEnumArgumentException

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

备注

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

下表描述 CheckBox 控件在其 NormalButton 样式控件 CheckBox.Appearance 处于不同状态下时的 System.Windows.Forms.Appearance

CheckState

Appearance.Normal

Appearance.Button

Checked

CheckBox 显示选中标记。

该控件显示凹下外观。

Unchecked

CheckBox 为空。

该控件显示凸起外观。

Indeterminate

CheckBox 显示一个选中标记并变灰。

该控件以平面显示。

示例

下面的代码示例将在一个标签中显示三个属性的值。连续单击该控件会使 ThreeState 属性交替设置为 truefalse,而 CheckAlign 会交替设置为 System.Drawing.ContentAlignmentMiddleRightMiddleLeft 值。此示例显示属性值如何在更改 ThreeState 属性和选中该控件时发生更改。此示例要求 CheckBoxLabelButton 已全部在窗体上实例化,并且标签的大小足以显示三行文本和对 System.Drawing 命名空间的引用。应当在该控件的 Click 事件处理程序中调用此代码。

Private Sub AdjustMyCheckBoxProperties()
    ' 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()
    
    ' 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
End Sub 'AdjustMyCheckBoxProperties
private void AdjustMyCheckBoxProperties()
 {
    // Concatenate the property values together on three lines.
    label1.Text = "ThreeState: " + checkBox1.ThreeState.ToString() + "\n" +
                  "Checked: " + checkBox1.Checked.ToString() + "\n" +
                  "CheckState: " + checkBox1.CheckState.ToString(); 
    
    // 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()
   {
      // 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()
{
    // Concatenate the property values together on three lines.
    label1.set_Text("ThreeState: " + checkBox1.get_ThreeState() + "\n"
        + "Checked: " + checkBox1.get_Checked() + "\n" 
        + "CheckState: " + checkBox1.get_CheckState());

    //Change the ThreeState and CheckAlign properties on every other click.
    if (!(checkBox1.get_ThreeState())) {
        checkBox1.set_ThreeState(true);
        checkBox1.set_CheckAlign(ContentAlignment.MiddleRight);
    }
    else {
        checkBox1.set_ThreeState(false);
        checkBox1.set_CheckAlign(ContentAlignment.MiddleLeft);
    }
} //AdjustMyCheckBoxProperties
private function AdjustMyCheckBoxProperties()
 {
    // Concatenate the property values together on three lines.
    label1.Text = "ThreeState: " + checkBox1.ThreeState.ToString() + "\n" +
                  "Checked: " + checkBox1.Checked.ToString() + "\n" +
                  "CheckState: " + checkBox1.CheckState.ToString(); 
    
    // 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;
    }
 }
 

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

CheckBox 类
CheckBox 成员
System.Windows.Forms 命名空间
CheckBox.Checked 属性
ThreeState