In depth analysis of C WinForm control Dock sequence adjustment

  • 2020-05-12 03:06:53
  • OfStack

Recently, I have been confused by the layout of the control in.net winform. Since the control is in Dock mode, it is easy to operate. If you maximize, you can adjust the size of the window, etc., you can change with the size of the window.
However, the problem is that the dock mode of.net winform is arranged according to the order of the control added first, Dock. Suppose I want to have three controls, A, B and C, to populate the window with Top, Bottom and Fill respectively, which means the window should look like this:
---------------------
A
---------------------
C
---------------------

----------------------

The question is, now I want to add the D control between A and C, what should I do? Tried a lot of methods are not working, 1 will overwrite the C control, which is the last added control.
Looking on the Internet, a lot of people put forward a helpless solution, that is, to do it all over again, to add the control to the window form again, although it can solve the confusion caused by the layout of Dock, but it brings a doubling of the workload and it is difficult to adjust to the same layout as before.
Considering that this is not the solution, add to add, I think that the first control to add has the priority of Dock, should be able to start from the Designer file ah.
Find the following code:

this.Controls.Add(this.D); 
this.Controls.Add(this.C);
this.Controls.Add(this.B);
this.Controls.Add(this.A);

Adjust the order of the code to:

this.Controls.Add(this.C); 
this.Controls.Add(this.D);
this.Controls.Add(this.B);
this.Controls.Add(this.A);

The problem is solved.
Thus, it can be seen that in the Desinger file, the controls added later are sorted by Dock with high priority, that is, the sorting priority of the A control is the highest.

Related articles: