CheckBox.Checked 属性

获取或设置一个值,该值指示 CheckBox 是否处于选中状态。

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

语法

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

value = instance.Checked

instance.Checked = value
[BindableAttribute(true)] 
public bool Checked { get; set; }
[BindableAttribute(true)] 
public:
property bool Checked {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_Checked ()

/** @property */
public void set_Checked (boolean value)
public function get Checked () : boolean

public function set Checked (value : boolean)

属性值

如果 CheckBox 处于选定状态,则为 true;否则为 false。默认值为 false

提示

如果将 ThreeState 属性设置为 true,则 Checked 属性将为 CheckedIndeterminateCheckState 返回 true

备注

如果该值为 true,则控件的 CheckBox 部分会显示一个选中标记。如果将 Appearance 属性设置为 Button,则当 Checked 设置为 true 时,控件将显示凹下外观;而当设置为 false 时,控件将呈现为像标准按钮一样的凸起外观。

示例

下面的代码示例将在一个标签中显示三个属性的值。连续单击该控件会使 ThreeState 属性交替设置为 truefalse,而 CheckAlign 会交替设置为 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 命名空间
ThreeState
CheckState