共用方式為


如何:使用 Windows Form CheckBox 控制項設定選項

Windows Forms CheckBox 控制項可為使用者提供 True/False 或「是/否」選項。 控制項經選取後會顯示核取記號。

使用 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;
          }
       }
    

另請參閱