如何:使用 Windows 窗体 CheckBox 控件设置选项

Windows 窗体 CheckBox 控件用于向用户提供 True/False 或 Yes/No 选项。 控件被选中时会显示一个复选标记。

使用 CheckBox 控件来设置选项

  1. 检查 Checked 属性的值以确定其状态,并使用该值设置选项。

    在下面的代码示例中,当 CheckBox 控件的 CheckedChanged 事件被引发时,如果复选框被选中,窗体的 AllowDrop 属性将被设置为 false。 这对于想要限制用户交互的情况非常有用。

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
       ' Determine the CheckState of the check box.
       If CheckBox1.CheckState = CheckState.Checked Then
          ' If checked, do not allow items to be dragged onto the form.
          Me.AllowDrop = False
       End If
    End Sub
    
    private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
    {
       // Determine the CheckState of the check box.
       if (checkBox1.CheckState == CheckState.Checked)
       {
          // If checked, do not allow items to be dragged onto the form.
          this.AllowDrop = false;
       }
    }
    
    private:
       void checkBox1_CheckedChanged(System::Object ^ sender,
          System::EventArgs ^ e)
       {
          // Determine the CheckState of the check box.
          if (checkBox1->CheckState == CheckState::Checked)
          {
             // If checked, do not allow items to be dragged onto the form.
             this->AllowDrop = false;
          }
       }
    

另请参阅