How to control docking order in WinForms

爱⌒轻易说出口 提交于 2019-11-30 04:09:10
serhio

Use these methods:

myControl.SendToBack();
myControl.BringToFront();
Stormenet

Go to View → Other windows → document outline.

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

Alexandre Pepin

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());
Oliver Hanappi

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

Gerrie Schenck

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

(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);

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

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

Use the FlowLayoutPanel it does exactly what you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!