[Yugong Series] Detailed explanation of GroupBox control on Winform control topic in September 2023


Preface

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 GroupBox control

The GroupBox control is a container control in Windows Forms that can be used to provide a visually grouped framework for a group of related controls. Other controls can be added to the GroupBox, such as Label, TextBox, Button, CheckBox, etc., to provide users with more intuitive and clear operation prompts.

The properties and methods of the GroupBox control are as follows:

Attributes:

  • Text: Gets or sets the title text of the GroupBox.
  • BackColor: Gets or sets the background color of the GroupBox.
  • ForeColor: Gets or sets the foreground color of the GroupBox.
  • Font: Get or set the font of the GroupBox.
  • Size: Get or set the size of the GroupBox.

method:

  • Contains: Determine whether a control is located in a GroupBox.
  • PerformLayout: Force GroupBox to redraw.

When using a GroupBox, you generally need to add it to the form first. You can add controls by dragging the GroupBox control onto the form in the designer, or you can add controls by instantiating the GroupBox object in code. For example:

GroupBox groupBox1 = new GroupBox();
groupBox1.Text = "个人信息";
groupBox1.Size = new Size(200, 100);
this.Controls.Add(groupBox1);

The above code adds a GroupBox control with a size of 200x100 and a title of "Personal Information" on the form.

Adding other controls to a GroupBox is the same as adding controls to a normal container control. For example, to add a Label and a Button to a GroupBox, the code is as follows:

Label label1 = new Label();
label1.Text = "姓名:";
label1.Location = new Point(20, 30);
groupBox1.Controls.Add(label1);

Button button1 = new Button();
button1.Text = "保存";
button1.Location = new Point(80, 60);
groupBox1.Controls.Add(button1);

The above code adds a Label and a Button control to groupBox1.

Through the GroupBox control, the controls on the form can be made more organized and clear, making it easier for users to operate.
Insert image description here

1. Introduction to attributes

1.1 FlatStyle

The GroupBox control is one of the commonly used container controls in Winform. It can be used to organize related controls together to form a logical group. Among them, the FlatStyle property is used to set the border style of the GroupBox. The optional values ​​are Flat, Popup and Standard.

  • Flat: Indicates that the GroupBox has no border, only a title;
  • Popup: indicates that the GroupBox has a raised border and the title is above the border;
  • Standard: Indicates that the GroupBox has a recessed border and the title is above the border.

Usage example:

// 创建一个GroupBox控件
GroupBox groupBox1 = new GroupBox();
groupBox1.Location = new Point(10, 10);
groupBox1.Size = new Size(200, 150);
groupBox1.Text = "选项";

// 设置边框样式为Flat
groupBox1.FlatStyle = FlatStyle.Flat;

// 添加一些控件到GroupBox中
Label label1 = new Label();
label1.Location = new Point(10, 20);
label1.Text = "选项1";

CheckBox checkBox1 = new CheckBox();
checkBox1.Location = new Point(10, 40);
checkBox1.Text = "是否选中";

groupBox1.Controls.Add(label1);
groupBox1.Controls.Add(checkBox1);

// 将GroupBox添加到窗体中
this.Controls.Add(groupBox1);

Insert image description here

The above code creates a GroupBox control with a Flat border style, which contains a Label control and a CheckBox control. The other two border styles are used in a similar way. You only need to change the value of the FlatStyle property to Popup or Standard.

2. Common scenarios

The GroupBox control is usually used to organize related controls in a form and display them in groups so that users can better understand and use them. Here are some common scenarios:

  1. Set tabs in the form: Place tabs of different categories in different GroupBoxes so that users can quickly find the required tabs.

  2. Collect user information: Place controls that input the same type of information, such as text boxes, drop-down lists, radio buttons, etc., in the same group so that users can see the information they need to fill in at a glance.

  3. Display the running status of the program: Place controls related to the running status in the same group, such as progress bars, text labels, buttons, etc., so that users can understand the current execution status of the program.

  4. Switching form layouts: When users switch form layouts, GroupBox can be used to easily classify and organize the controls in the form to better adapt to different screen sizes and resolutions.

In these scenarios, GroupBox can make the form clearer and easier to use, improving user experience.

3. Specific cases

The following is a complete example of the GroupBox control in Winform:

  1. Create a new Winform project and change the form's name to "GroupBoxDemo".

  2. Drag and drop a GroupBox control on the form and change its name to "groupBox1".

  3. Add three RadioButton controls in the GroupBox control and change their names to "radioButton1", "radioButton2" and "radioButton3" respectively.

  4. Set the text separately for each RadioButton control, such as "Option 1", "Option 2" and "Option 3".

  5. Add the following code in the form's Load event:

private void GroupBoxDemo_Load(object sender, EventArgs e)
{
    
    
    // 将第一个RadioButton控件设置为选中状态
    radioButton1.Checked = true;
}
  1. Add the following code in the CheckedChanged event of each RadioButton control:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    
    
    if (radioButton1.Checked)
    {
    
    
        MessageBox.Show("您选择了选项1。");
    }
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
    
    
    if (radioButton2.Checked)
    {
    
    
        MessageBox.Show("您选择了选项2。");
    }
}

private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
    
    
    if (radioButton3.Checked)
    {
    
    
        MessageBox.Show("您选择了选项3。");
    }
}
  1. Run the program, click each RadioButton control, and observe whether the corresponding message box pops up.

After completing the above steps, you will see a complete case of the GroupBox control. In this case, we created a simple interface that contains three RadioButton controls and a GroupBox control. Whenever the user selects a RadioButton control, the program will pop up a prompt box to inform the user which option they selected. This is a basic example of a GroupBox control that you can modify and extend as needed.

Insert image description here

Guess you like

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