92

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
  • 16,932
  • 15
  • 95
  • 160
  • Thank you; I needed to programmatically control the docking and I didn't even realize that their order controlled how they docked. – Eagle-Eye May 23 '14 at 15:11

8 Answers8

164

Go to View → Other windows → document outline.

In that window drag the controls so the docking is as you like it to be.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Stormenet
  • 25,926
  • 9
  • 53
  • 65
81

Use these methods:

myControl.SendToBack();
myControl.BringToFront();
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
serhio
  • 28,010
  • 62
  • 221
  • 374
  • 2
    I was looking for `control.SendToFront()` See, now that was easy. Thanks! – caesay Apr 09 '10 at 13:25
  • 11
    @Tommy: Do you typically program from behind your computer? :) – Ashley Tate Jan 13 '11 at 21:36
  • 7
    On window from design, right click. select sendToBack and Right click again: BringToFont – Wolf Oct 12 '16 at 04:26
  • 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". – Anders Lindén Jun 20 '17 at 09:34
13

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());
sashoalm
  • 75,001
  • 122
  • 434
  • 781
Alexandre Pepin
  • 1,816
  • 3
  • 20
  • 36
6

A control has two methods to achieve what you are looking for: BringToFront and SendToBack.

caesay
  • 16,932
  • 15
  • 95
  • 160
Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
4

The order in which the controls are being added to the Controls collection determines the docking order.

Oliver Hanappi
  • 12,046
  • 7
  • 51
  • 68
4

Note that when doing this programmatically, then there's a very easy way to achieve this, namely:

containerPanel.Controls.SetChildIndex(Element, 0); //sends element to the bottom of the list
Jack
  • 871
  • 1
  • 9
  • 17
3

(For the sake matter of showing another option): In Visual Studio 2012 (and later):

  1. Select the Control you want to Move to the Front (or back);
  2. Click in the below marked buttons (Bring to Front / Send to Back); enter image description here

This will give you the possibility to rearrange the Controls to your desired order.

Community
  • 1
  • 1
Arthur Zennig
  • 2,058
  • 26
  • 20
-4

Use the FlowLayoutPanel it does exactly what you want.

jhersey29
  • 959
  • 6
  • 6
  • The answer could use more details, however a fresh take or opinion on how to organise panels doesn't need to be down-voted this hard. A FlowLayoutPanel or SplitLayout COULD be a nicer way to handle this. Instead of having the order controlled "behind the scenes" in a weird way by winForms. – 2b77bee6-5445-4c77-b1eb-4df3e5 May 08 '23 at 16:30