[Yugong Series] Detailed Explanation of CheckBox Controls on Winform Controls in September 2023


foreword

Winform controls are user interface elements in Windows Forms. They can be used to create various visual and interactive components of Windows applications, such as buttons, labels, text boxes, drop-down list boxes, check boxes, radio boxes, progress bars, etc. . Developers can use Winform controls to build user interfaces and respond to user actions to create powerful desktop applications.

1. Detailed explanation of CheckBox control

The CheckBox control is one of the commonly used user interface controls in Winform, and it is usually used to represent a binary state (checked or unchecked). In Winform, we can create and use CheckBox controls through the following steps:

  1. In Visual Studio's design view, drag a CheckBox control from the Toolbox onto the window.
  2. Double-click the CheckBox control to open its property window, and set the properties of the control such as Text, Name, and Checked.
  3. Add methods for handling CheckBox control events, such as the CheckedChanged event, in the code, so as to perform specific logic operations when the CheckBox state changes.

The following is a simple example that demonstrates how to add a handler for the CheckedChanged event to the CheckBox control:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    
    
    if (checkBox1.Checked)
    {
    
    
        // CheckBox被选中的逻辑操作
    }
    else
    {
    
    
        // CheckBox未被选中的逻辑操作
    }
}

In the above code, we check whether the CheckBox is selected through the Checked property. If the Checked property is true, it means that the CheckBox is selected, otherwise it means that the CheckBox is not selected. When the CheckBox state changes, the CheckedChanged event will be triggered and the corresponding logic operation will be performed.

1. Attribute introduction

1.1 AutoCheck

The AutoCheck property of the CheckBox control is used to specify whether to automatically check the option, that is, whether to allow the user to change the Checked property when clicking the control. By default, the AutoCheck property is true, that is, when the user clicks the CheckBox control, the Checked property of the CheckBox will be automatically changed.

Here is a simple example showing how to use the AutoCheck property:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    
    
    if (checkBox1.Checked)
    {
    
    
        // CheckBox被选中的逻辑操作
    }
    else
    {
    
    
        // CheckBox未被选中的逻辑操作
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    
    
    checkBox1.AutoCheck = false; // 禁用自动检查
}

In the above code, we set the AutoCheck property to false in the Form1_Load event handler, so when the CheckBox control is clicked, the Checked property does not change automatically. We also added a CheckedChanged event handler to perform our own logic when the CheckBox state changes.

Note that if you disable the AutoCheck property, you will need to explicitly change the Checked property in code, or manually change the Checked property in the CheckedChanged event handler, to ensure that the CheckBox's state is updated correctly.

1.2 ThreeState

The CheckBox control property ThreeState in Winform indicates whether to enable the three-state function. By default, the ThreeState property is false, which means that the CheckBox has only two states: selected or unselected. However, if this property is set to true, the CheckBox will have three states: selected, unselected, or half-selected.

When the ThreeState property is true, the Checked property of CheckBox will no longer be true or false, but an enumeration type CheckState, which contains three values:

  • Checked: Indicates the selected state;
  • Indeterminate: Indicates the half-selected state;
  • Unchecked: Indicates the unchecked state.

When the CheckBox is half-selected, its state can be changed through a program, for example:

checkBox1.CheckState = CheckState.Indeterminate;

It should be noted that, in order to protect the user's selection of the CheckBox state, when ThreeState is set to true, only the CheckState property can be used to operate the CheckBox state, and the Checked property cannot be used directly. When judging the CheckBox status in the code, you can use the following code:

if (checkBox1.CheckState == CheckState.Checked)
{
    
    
    // 选中
}
else if (checkBox1.CheckState == CheckState.Indeterminate)
{
    
    
    // 半选中
}
else
{
    
    
    // 未选中
}

2. Common scenarios

The CheckBox control is one of the commonly used controls in Winform and is often used in the following scenarios:

  1. Provide options in settings: The CheckBox control can be used to provide a series of options that the user can select or deselect as needed. For example, in the software settings, users can choose whether to enable the automatic update function.

  2. Single Option Selection in Forms: In forms, the CheckBox control can be used to allow the user to select a single option. For example, in a registration form, the user can choose whether to agree to the terms and conditions of use.

  3. Perform batch operations: CheckBox control can be used to perform batch operations, for example, select multiple files to copy, move, delete and other operations.

  4. Selection in search and filter: The CheckBox control can be used to select in search and filter, for example, in a music player, users can select different music genres to filter their playlist.

  5. Select in a multi-select list: CheckBox controls can be used to select in a multi-select list. For example, in a shopping cart, the user can select some items and use a CheckBox control to select them.

3. Specific cases

The following is a specific case of using the CheckBox control in a simple Winform.

Let's say we have an app where the user can choose their favorite color. We can use multiple checkbox controls to achieve this functionality. Here are the implementation steps:

  1. Create a new Winform application in Visual Studio.

  2. In Design view, drag a CheckBox control from the Toolbox onto the form.

  3. Change the Text property of the CheckBox control to "red" and the Name property to "chkRed".

  4. Copy and paste the Checkbox controls, change their Text properties to "Green" and "Blue" respectively, and change their Name properties to "chkGreen" and "chkBlue" respectively.

  5. Double-click any checkBox control on the form, and add the following code in the click event:

private void chkRed_CheckedChanged(object sender, EventArgs e)
{
    
    
    if (chkRed.Checked)
    {
    
    
        MessageBox.Show("您选择了红色!");
    }
}
  1. Repeat step 5 to add corresponding events for the other two checkBox controls.

  2. Now when the user selects one or more colors, a message box appears.

This is a simple use case of the CheckBox control in Winform. You can extend and modify this example as needed to suit your needs.

Guess you like

Origin blog.csdn.net/aa2528877987/article/details/132634164