How to control docking order in WinForms

2019-02-03 21:24发布

As the title states, I'm looking for a way to control the order in which the items dock to the top of my control.

I've played with the windows form designer, and i cant seem to find what the RightClick->Order->SendToFront is doing, because thats exactly what I want to happen. As far as I can get to happen, as I add my contents to my control, the newest contents is always at the top, and I'd like for the Newer contents to be on the bottom, and the oldest contents to be at the top.

Summary: Is there an easy way in WinForms (C#), to control the order in which things dock to the sides of controls?

Edit: I have to do this from the code behind as i add my controls, I can't use any features of the WinForm's designer, but what I am trying to do, is mimic the WinForms Designer SendToBack and SendToFront features

7条回答
劳资没心,怎么记你
2楼-- · 2019-02-03 21:38

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

查看更多
ら.Afraid
3楼-- · 2019-02-03 21:43

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

查看更多
Fickle 薄情
4楼-- · 2019-02-03 21:44

Use these methods:

myControl.SendToBack();
myControl.BringToFront();
查看更多
一纸荒年 Trace。
5楼-- · 2019-02-03 21:49

Go to View → Other windows → document outline.

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

查看更多
男人必须洒脱
6楼-- · 2019-02-03 21:50

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());
查看更多
SAY GOODBYE
7楼-- · 2019-02-03 21:55

Use the FlowLayoutPanel it does exactly what you want.

查看更多
登录 后发表回答