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

Panel is one of the commonly used container controls in Winform, which can be used to accommodate other controls, such as Label, Button, etc. The main function of the Panel control is to place multiple controls in the same visual context. For example, you can place multiple buttons and text boxes on the Panel, and then set the background color and border of the Panel, which can make these controls more visually unified. , more beautiful.

Common properties of Panel controls:

  1. BackColor: Set the background color of the Panel control.

  2. BorderStyle: Set the border style of the Panel control.

  3. Dock: Set the docking mode of the Panel control. Common ones include Top, Bottom, Left, Right, Fill, etc.

  4. Enabled: Sets whether the Panel control is available.

  5. Visible: Set whether the Panel control is visible.

  6. Size: Set the size of the Panel control.

  7. Location: Set the location of the Panel control.

The Panel control has many other properties and methods that can be used flexibly according to actual needs.

1. Introduction to attributes

1.1 GrowAndShrink和GrowOnly

The GrowAndShrink property and GrowOnly property of the Panel control are both properties that control the size change of the Panel control. The specific uses are as follows:

  1. GrowAndShrink property: When the size of the child controls in the Panel control changes, the Panel control will automatically adjust its size to adapt to the child controls. When child controls increase or decrease, the Panel control will increase or decrease accordingly to ensure that scroll bars do not appear and that it remains appropriately sized.

  2. GrowOnly property: Similar to the GrowAndShrink property, but only allows the Panel control to increase in size in one direction. For example, if the docking mode of the Panel control is Top, the Panel control is only allowed to increase in size downward, but not upward or in other directions. This keeps the position of the Panel control unchanged and only increases the height or width.

It should be noted that these two properties will only take effect when the Dock property is set to one of Top, Bottom, Left, Right, and Fill. If the Dock property is set to None, these two properties have no effect.

For example, if you place a Panel control in a form, set the Dock property of the Panel control to Fill, and include several sub-controls, if you need the Panel control to dynamically change with the size of the sub-controls, you can set GrowAndShrink The property is true. If you only need its height or width to grow dynamically, you can set the GrowOnly property to true.

2. Common scenarios

Common scenarios for Panel controls in Winform include:

  1. As a container control, other controls can be added as sub-controls to implement layout and grouping functions.

  2. As a scroll bar container, when there are too many sub-controls or exceed the visible range of the Panel control, the Panel control can automatically appear scroll bars, allowing users to browse and operate all sub-controls.

  3. As a container for drawing graphics, you can draw custom graphics on the Panel control, such as drawing curve charts, bar charts, etc.

  4. As a card layout control, you can set up multiple Panel controls. Each Panel control represents a card. By switching the display and hiding of the Panel control, the card switching effect is achieved.

  5. As a container for dynamically adding controls, you can dynamically add sub-controls through code to achieve the function of dynamic loading and deletion of controls.

3. Specific cases

The following is a complete case using the Panel control in Winform:

  1. Create a new Windows Forms application in Visual Studio.

  2. Add a Panel control to the form and set the size and position of the control.

  3. Add other controls (such as buttons, labels, text boxes, etc.) to the Panel.

  4. Add code in the Form_Load event handler to set the properties of the Panel, for example:

private void Form1_Load(object sender, EventArgs e)
{
    
    
    panel1.BackColor = Color.White; //设置Panel的背景颜色
    panel1.AutoScroll = true; //启用Panel的自动滚动功能
}
  1. Add scroll bars to the Panel so users can scroll the content in the Panel. You can use the ScrollBar control to achieve this. For example, add the following code in the Form_Load event handler:
ScrollBar vScrollBar = new VScrollBar(); //创建垂直滚动条
vScrollBar.Dock = DockStyle.Right; //将滚动条停靠在Panel的右侧
panel1.Controls.Add(vScrollBar); //将滚动条添加到Panel中
  1. Handle the scrollbar's events so that when the user scrolls the Panel, its content moves accordingly. For example, you can use the following code to scroll controls in a Panel up or down:
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
    
    
    panel1.VerticalScroll.Value = e.NewValue; //将Panel的垂直滚动位置设置为滚动条的值
}

This is a simple and complete case of using the Panel control in Winform. By using the Panel control, you can easily create a scrollable area with scrolling capabilities and add other controls within it.

Insert image description here

Guess you like

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