How to use the CheckBox control in WinForms?

To use a CheckBox control in WinForms, you first need to drag a CheckBox control onto the form or create one dynamically through code. You can then use the CheckBox control in several ways:

  1. Set the Text property of the CheckBox to display a text label.
checkBox1.Text = "Check me";
  1. Use the CheckedChanged event of the CheckBox to handle changes in its checked state.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox1.Checked)
    {
        // 勾选时的操作
    }
    else
    {
        // 取消勾选时的操作
    }
}
  1. Set the CheckBox’s Checked property to either set or retrieve the check status.
if (checkBox1.Checked)
{
    // 勾选状态
}
else
{
    // 未勾选状态
}
  1. Use the CheckState property of the CheckBox to set or retrieve the three possible states of the checkmark (Checked, Unchecked, Indeterminate).
if (checkBox1.CheckState == CheckState.Checked)
{
    // 勾选状态
}
else if (checkBox1.CheckState == CheckState.Unchecked)
{
    // 未勾选状态
}
else if (checkBox1.CheckState == CheckState.Indeterminate)
{
    // 未确定状态
}

Using the above method, it is possible to implement the checking operation in WinForms with the CheckBox control and perform corresponding logical processing based on the checked status.

Leave a Reply 0

Your email address will not be published. Required fields are marked *