Поделиться через


How to: Set Options with Windows Forms CheckBox Controls

A Windows Forms CheckBox control is used to give users True/False or Yes/No options. The control displays a check mark when it is selected.

To set options with CheckBox controls

  • Examine the value of the Checked property to determine its state, and use that value to set an option.

    In the code sample below, when the CheckBox control's CheckedChanged event is raised, the form's AllowDrop property is set to false if the check box is checked. This is useful for situations where you want to restrict user interaction.

    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.get_CheckState() == CheckState.Checked  ) 
       {
          // If checked, do not allow items to be dragged onto the form. 
          this.set_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;
          }
       }
    

See Also

Tasks

How to: Respond to Windows Forms CheckBox Clicks

Reference

CheckBox Control Overview (Windows Forms)

CheckBox

Other Resources

CheckBox Control (Windows Forms)