[Yugong Series] Detailed explanation of FlowLayoutPanel control on Winform control special 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 FlowLayoutPanel control

The FlowLayoutPanel control is a container control in Winform, used to automatically arrange its child controls in a scrollable panel. It helps users easily design complex layouts and adjust sizes automatically. The characteristics of the FlowLayoutPanel control are as follows:

  • Compared with other container controls, FlowLayoutPanel is more suitable for hosting controls with dynamic nature;
  • You can control the arrangement direction of controls by setting the flow direction to "left to right", "top to bottom", "right to left", "bottom to top", etc.;
  • You can enable automatic scroll bar functionality by setting the AutoScroll property of the FlowLayoutPanel control.

Use the FlowLayoutPanel control to effectively manage multiple controls, especially for scenarios that require dynamic addition of controls, such as dynamically generated charts, standard controls for flow layout, etc. The FlowLayoutPanel control can also be used in conjunction with other Winform controls when implementing specific functions.

1. Introduction to attributes

1.1 FlowDirection

The FlowDirection property of the FlowLayoutPanel control is used to set the arrangement direction of sub-controls in the control. The FlowDirection property has four enumeration values:

  • LeftToRight: Arrange child controls from left to right.
  • TopDown: Arrange child controls from top to bottom.
  • RightToLeft: Arrange child controls from right to left.
  • BottomUp: Arrange child controls from bottom to top.

For example, if you want to arrange the child controls in the FlowLayoutPanel from left to right, you can set it like this:

flowLayoutPanel.FlowDirection = FlowDirection.LeftToRight;

If you want to arrange the sub-controls in the FlowLayoutPanel from right to left, you can set it like this:

flowLayoutPanel.FlowDirection = FlowDirection.RightToLeft;

By setting the FlowDirection property, you can easily implement different control arrangements to meet different layout needs. When using the FlowLayoutPanel control, you need to select the appropriate FlowDirection property value based on actual needs.

1.2 WrapContents

The FlowLayoutPanel control is one of the commonly used container controls in Winform. It can arrange sub-controls according to flow layout. Among them, the WrapContents property is the property that controls whether the sub-control wraps.

When the WrapContents property is set to True, if the total width of the sub-controls exceeds the width of the FlowLayoutPanel control, the sub-controls will automatically wrap and arrange. When the WrapContents property is set to False, the total width of the sub-control exceeds the width of the FlowLayoutPanel control, which will cause the sub-control to be cropped or partially obscured.

Use the WrapContents property to easily control the layout effect of the FlowLayoutPanel control. For example, you can place multiple buttons in the form and set WrapContents to True in the FlowLayoutPanel control. When the form size changes, the buttons will automatically be arranged to the next row to adapt to the form size.

2. Common scenarios

The FlowLayoutPanel control is often used in scenarios where multiple controls need to be dynamically added and automatically arranged, for example:

  1. Data list: Bind multiple pieces of data to the FlowLayoutPanel. Each piece of data contains multiple controls (such as Label, Button, etc.). The FlowLayoutPanel will automatically arrange the controls according to the size and spacing of the controls to achieve a simple data list display.

  2. Picture wall: Dynamically add multiple pictures to the FlowLayoutPanel, set the picture size and spacing, and the FlowLayoutPanel will automatically arrange the pictures to achieve a beautiful picture wall effect.

  3. Form input: Add multiple form controls (such as Label, TextBox, ComboBox, etc.) to FlowLayoutPanel, and FlowLayoutPanel will automatically arrange these controls to implement a simple form input page.

  4. Menu navigation: Add multiple menu items to FlowLayoutPanel, set the menu size and spacing, and FlowLayoutPanel will automatically arrange the menu items to implement simple menu navigation.

The FlowLayoutPanel control can easily realize the function of dynamically arranging multiple controls, improving the flexibility and user experience of Winform applications.

3. Specific cases

The following is a case of adding, deleting, sorting and selecting FlowLayoutPanel control elements in Winform:

Step 1: Create FlowLayoutPanel and add buttons

Add a FlowLayoutPanel control and four buttons to the Winform interface, namely add, delete, sort, and reverse order. The button's Click event needs to be bound to the corresponding method.

Step 2: Define global variables and initialize

Define the global variable count to record the number of controls in the FlowLayoutPanel. The control names are "Button 1", "Button 2", "Button 3", and "Button 4". Add initialization code in the Form_Load event:

private int count = 4;
private string[] controlNames = {
    
     "Button 1", "Button 2", "Button 3", "Button 4" };

private void Form1_Load(object sender, EventArgs e)
{
    
    
    for (int i = 0; i < count; i++)
    {
    
    
        Button btn = new Button();
        btn.Name = controlNames[i];
        btn.Text = controlNames[i];
        btn.Width = 100;
        btn.Height = 50;
        btn.Click += Btn_Click;
        flowLayoutPanel1.Controls.Add(btn);
    }
}

During initialization, use a for loop to create four buttons and add them to the FlowLayoutPanel control. At the same time, add a Click event for each button and bind the Btn_Click method.

Step 3: Add Button

In the method of adding a button, maintain the number of controls through the count variable, use the controlNames array to record the control name, create a new button, add it to the FlowLayoutPanel control, and update the count variable.

private void btnAdd_Click(object sender, EventArgs e)
{
    
    
    Button btn = new Button();
    btn.Name = "Button " + (++count);
    btn.Text = btn.Name;
    btn.Width = 100;
    btn.Height = 50;
    btn.Click += Btn_Click;
    flowLayoutPanel1.Controls.Add(btn);
}

Step 4: Delete Button

In the method of deleting a button, traverse all the buttons in the FlowLayoutPanel control, find the corresponding button and delete it. When deleting, be aware that the number of buttons and the count variable in the FlowLayoutPanel control need to be updated accordingly.

private void btnDelete_Click(object sender, EventArgs e)
{
    
    
    foreach (Control control in flowLayoutPanel1.Controls)
    {
    
    
        if (control is Button && control.Name == controlNames[count - 1])
        {
    
    
            flowLayoutPanel1.Controls.Remove(control);
            count--;
            break;
        }
    }
}

Step 5: Sort and Reverse Order

In the method of sorting and reversing buttons, first sort the button names in the FlowLayoutPanel control in alphabetical order or in reverse order, and then rearrange the buttons through the Sort method.

private void btnSort_Click(object sender, EventArgs e)
{
    
    
    controlNames = controlNames.OrderBy(n => n).ToArray();
    for (int i = 0; i < count; i++)
    {
    
    
        flowLayoutPanel1.Controls[i].Name = controlNames[i];
        flowLayoutPanel1.Controls[i].Text = controlNames[i];
    }
    flowLayoutPanel1.Controls.SetChildIndex(flowLayoutPanel1.Controls[0], 0);
}

private void btnReverse_Click(object sender, EventArgs e)
{
    
    
    controlNames = controlNames.OrderByDescending(n => n).ToArray();
    for (int i = 0; i < count; i++)
    {
    
    
        flowLayoutPanel1.Controls[i].Name = controlNames[i];
        flowLayoutPanel1.Controls[i].Text = controlNames[i];
    }
    flowLayoutPanel1.Controls.SetChildIndex(flowLayoutPanel1.Controls[0], 0);
}

Step 6: Check the button

In the method of selecting a button, obtain the currently clicked button through the sender parameter, set its text to red, traverse all the buttons in the FlowLayoutPanel control, and set the text color of the remaining buttons to black.

private void Btn_Click(object sender, EventArgs e)
{
    
    
    Button btn = sender as Button;
    btn.ForeColor = Color.Red;
    foreach (Control control in flowLayoutPanel1.Controls)
    {
    
    
        if (control is Button && control != btn)
        {
    
    
            control.ForeColor = Color.Black;
        }
    }
}

Insert image description here

Guess you like

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