How to control docking order in WinForms

62,251

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.

Share:
62,251
caesay
Author by

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&lt;a&gt;caesay.com

Updated on July 08, 2022

Comments

  • caesay
    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
    caesay about 14 years
    I need to edit the docking order after the controls are added to the collection
  • Ryan
    Ryan about 14 years
    Then go for the ControlCollection.SetChildIndex(control, index) method.
  • caesay
    caesay about 14 years
    I was looking for control.SendToFront() See, now that was easy. Thanks!
  • Ashley Tate
    Ashley Tate over 13 years
    @Tommy: Do you typically program from behind your computer? :)
  • Coops
    Coops over 12 years
    I was wondering what words I could use to describe that exact window, get there eventually on Google, Thank you!
  • Xinchao
    Xinchao almost 11 years
    I think this should be the answer
  • Wayne
    Wayne almost 10 years
    This does not work if controls are added dynamically.
  • Hao Nguyen
    Hao Nguyen almost 9 years
    Solve my problem easily at design time.
  • user2366975
    user2366975 almost 9 years
    @Oliver Hanappi: Is it possible to do this in the designer? Without code?
  • Ryan
    Ryan almost 9 years
    You should be able to reorder controls in the Document Outline window (blogs.msdn.com/b/zainnab/archive/2010/11/30/…)
  • caesay
    caesay about 8 years
    This post doesn't answer the question, and this question is 6 years old with relevant answers already.
  • Grey Wolf
    Grey Wolf over 7 years
    On window from design, right click. select sendToBack and Right click again: BringToFont
  • Anders Lindén
    Anders Lindén over 7 years
    I 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
    Anders Lindén about 7 years
    This 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 years
    Thanks. this is what i was searching for.
  • soulshined
    soulshined almost 6 years
    8 years later... best answer
  • Matthew Young
    Matthew Young over 4 years
    This is the one! Just drag around the order and it works perfectly, what a great tip!