How to adjust the Dock order of controls in C#

How to adjust the Dock order of controls in C#

In the C#Winform form, the order behind the Dock of the controls is prioritized according to the order in which the controls are added . Assuming that three Button buttons A, B, and C are added to the Panel container in order and their Dock mode is set to top, the order after the Dock should be as follows:

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

A

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

B

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

C

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

At this time, you need to add a fourth Button button and want to place it between the B and C buttons. The most stupid way is to delete the control and start over. Here is a simple and effective method:

(1) Add the fourth Button button D in the Panel control;

(2) Find the Designer.cs file of the form in the Solution Explorer and open it, and find the following code in it:

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

(3) Adjust the sequence of codes to:

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

(4) Finally, set the Dock mode of button D to top.

to sum up:

Assuming that the order of adding controls is: A, B, C, then the code in the Designer.cs file is:

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

According to the order of addition, first is A, Dock first, then B, and finally C. If you want to insert controls in A, B, and C later, you can change the order of addition in the Designer.cs file.

Guess you like

Origin blog.csdn.net/Kevin_Sun777/article/details/109293924