简体   繁体   中英

How to control docking order in WinForms

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?

Go to View → Other windows → document outline.

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

Use these methods:

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

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

控件有两种方法可以实现您要查找的内容: BringToFrontSendToBack

控件添加到 Controls 集合的顺序决定了停靠顺序。

(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.

请注意,以编程方式执行此操作时,有一种非常简单的方法可以实现此目的,即:

containerPanel.Controls.SetChildIndex(Element, 0); //sends element to the bottom of the list

使用 FlowLayoutPanel 它完全符合您的要求。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM