It is tricky for many developers who are new to winforms programming, that is, how to keep their forms synchronized when users adjust various controls and make them commensurate with the parent form. This can be a very frustrating situation, especially for developers who have a web programming background and have switched to winforms.
To alleviate this problem, the. NET framework allows you to set properties for child controls. How do commands work when you adjust the size of the parent form. The two attributes used for the command control to adjust the hour action are "Dock" and "anchor ".
The dock and anchor connect controls to a location in their parent form, eliminating the trouble of having an unpredictable interface for applications. The best thing is that you don't need any handwriting code to set up these attributes. All things can be done through vertices and clicks in Visual Studio IDE.
Anchor attributes
As the name implies, this property forces the control to position itself in a relative or absolute position in the parent form or parent control. This attribute has four values that can be enabled or disabled:
- Top--Indicates that the top of the control related to the parent form (or parent Control) should be fixed.
- Bottom--Indicates that the edges related to the parent form (or parent control) in the control should be fixed.
- Left--Indicates that the left edge of the control related to the parent form (or parent Control) should be fixed.
- Right--Indicates that the right edge of the control related to the parent form (or parent Control) should be fixed.
To set the anchor attribute for a control, select the control in the Visual Studio designer and go to the Properties window. You will see an attribute marked as "anchor. Click the property value section and a small window appears asking you to select the anchor you want to assign the control.ChartAThe anchor setting window of "top side and left side" is selected.ChartBSelect the "bottom and right" window.
Chart |
|
Anchor tool top and left |
Chart B |
|
Anchor tool bottom and right |
In Visual Studio, when a control is placed on a form, the default anchor is set to "top and left", which makes the control and the top and left edges of the form fixed.
Only when you discover the impact of different anchor settings on the control can you realize the significance of anchor. The image below will be helpful.
ChartCThe following is a form with ten child controls. Each sub-control has different anchor attribute values and uses its anchor to set the annotation. The dark red box behind the gray-white control is another sub-control-its anchor attribute is set to top, bottom, left, and right.ChartDThis is the same form after the region is increased.
As you can see, each control automatically maintains its position in the parent form. We didn't write code to complete this; we simply set the anchor attribute of the control.
There are several important points to mention. If you do not specify that a widget is left or right-anchored, it retains a relative left/right position in the parent form. If you do not specify whether a control has a top or bottom pin, the same is true. A good example of this is the "no anchor" control. Such a control does not have an anchoring value, so it just floats in the center of the form.
The other extreme is to select controls with all anchor values (top, bottom, left, right ). For this,ChartCAndChartDThe dark red square visible object behind other controls is an example. When all the anchor values are selected, the control is only adjusting the size of the parent form in hours, with the increase and contraction-compared with the edge of the form, all its edges remain static.
Dock Properties
The dock property forces the control to stay close to an edge of the parent form (or control. Although the anchor attribute can also achieve this, the dock attribute allows you to allow sub-forms to "stack" on top (or next to each other) in the parent form ". If the size of a subform changes, the other subforms that are parked next to it will also change.
Unlike the anchor attribute, you can set the dock attribute to a single value. The valid values are as follows:
- Top--Forces the control to be at the top of the parent form (or control. If other child controls with the same parent form are also set to stop at the top, the controls are stacked above each other.
- Bottom--Forces the control to be at the bottom of the parent form (or control. If other child controls with the same parent form are also set to stop at the bottom, the controls are stacked above each other.
- Left--Forces the control to be on the left of the parent form (or control. If other child controls with the same parent form are also set to stop on the left, the controls are stacked next to each other.
- Right--Forces the control to be on the right of the parent form (or control. If other child controls with the same parent form are also set to stop on the right side, the controls are stacked next to each other.
- Fill--Forces the control to be above the parent form (or control. If other child controls with the same parent form are also set to stop at the top, the controls are stacked above each other.
- None--Indicates that the control will run normally.
To set the dock value of a control, select the control in Visual Studio and go to the Properties window. You will see an attribute marked as "Dock. Click the value of this attribute to display a small window asking you to specify how the control will be stopped. This form is displayed in (ChartE, ChartFAnd chartsG):
Chart G |
|
Select Stop at top |
Like the anchor attribute, you don't realize their significance until you find it works.ChartHThe following figure shows a form with five child controls. Each child form has different dock values.
Chart H |
|
Five child controls with different dock values |
ChartIAndChartHThe same window, except that the window has now been adjusted to a larger trajectory.
Chart I |
|
Greater trajectory |
ChartJIt also shows the sumChartHIn the same window, the controls at the bottom, top, left, and right of the form become smaller. Note that the control located in the middle of the form is set as the Dock Fill, which automatically increases.
Chart J |
|
Smaller trajectory |
Remember that for the dock attribute, the order in which controls are added will affect the way they are stopped. For example, if you add control A to the form and instruct it to stop filling, then you add control B to the form and instruct it to stop at the top, control B overwrites the upper part of control. The reason is that control B is considered to be in front of control a because it is added after control.
To solve this problem, you must right-click control a in Visual Studio and select "bring to front" in the context menu )". In this way, control a can appear in front of control B, and the control can operate as expected.