How to control docking order in WinForms
Solution 1
Use these methods:
myControl.SendToBack();
myControl.BringToFront();
Solution 2
Go to View → Other windows → document outline.
In that window drag the controls so the docking is as you like it to be.
Solution 3
As you said, the newest control added to the controls collection is the one on top. If you need a newer control to be added at the bottom, I'll suggest to create a list of controls, add the controls to the list, reverse the list and add the list to the controls collection.
List<Control> controls = new List<Control();
controls.Add(new myFirstControl());
controls.Add(new mySecondControl());
controls.Reverse();
this.Controls.AddRange(controls.ToArray());
Solution 4
A control has two methods to achieve what you are looking for: BringToFront
and SendToBack
.
Solution 5
The order in which the controls are being added to the Controls collection determines the docking order.
![caesay](https://i.stack.imgur.com/eooRi.png?s=256&g=1)
caesay
I'm in a committed relationship with good code, so if we go out it will only be as friends. Feel free to contact me by email at mk.stackexchange<a>caesay.com
Updated on July 08, 2022Comments
-
caesay almost 2 years
I'm looking for a way to control the order in which the items dock to the top of my control.
I've noticed as I add children to my control (in the designer, or through code), the newest child is always at the top. I'd like for the newer children to be on the bottom, and the oldest to be at the top.
Is there a way to do this through code? In the WinForms designer,
RightClick->Order->BringToFront
/SendToBack
is doing something similar to what I want to do, but how can this be done programmatically? -
caesay about 14 yearsI need to edit the docking order after the controls are added to the collection
-
Ryan about 14 yearsThen go for the
ControlCollection.SetChildIndex(control, index)
method. -
caesay about 14 yearsI was looking for
control.SendToFront()
See, now that was easy. Thanks! -
Ashley Tate over 13 years@Tommy: Do you typically program from behind your computer? :)
-
Coops over 12 yearsI was wondering what words I could use to describe that exact window, get there eventually on Google, Thank you!
-
Xinchao almost 11 yearsI think this should be the answer
-
Wayne almost 10 yearsThis does not work if controls are added dynamically.
-
Hao Nguyen almost 9 yearsSolve my problem easily at design time.
-
user2366975 almost 9 years@Oliver Hanappi: Is it possible to do this in the designer? Without code?
-
Ryan almost 9 yearsYou should be able to reorder controls in the Document Outline window (blogs.msdn.com/b/zainnab/archive/2010/11/30/…)
-
caesay about 8 yearsThis post doesn't answer the question, and this question is 6 years old with relevant answers already.
-
Grey Wolf over 7 yearsOn window from design, right click. select sendToBack and Right click again: BringToFont
-
Anders Lindén over 7 yearsI found out that the windows are listed in reverse order. If I just drag around components, they will parent each other, which I did not want. What I had to do what to drag controls one after another to the top of the list of controls of the same parent, in reverse order to have the components in the desired order.
-
Anders Lindén about 7 yearsThis method also applies to adding docking controls in the form designer. With the context menu, you can send to back, then bring to front then dock and it will be the last control in the docking "chain".
-
ناصر عبده عبده محمد almost 7 yearsThanks. this is what i was searching for.
-
soulshined almost 6 years8 years later... best answer
-
Matthew Young over 4 yearsThis is the one! Just drag around the order and it works perfectly, what a great tip!