在用户控件C#.NET添加/对接控制控件、用户、NET

2023-09-06 16:26:38 作者:月夜下的思念

我写一个用户控件添加了子控件编程。目前,我加入新的控件,像这样:

I am writing a UserControl which adds child controls programmatically. Currently I am adding new controls like so:

this.Controls.Add(new Control() { Height = 16, Dock = DockStyle.Top });

我遇到的问题是,新的控制是上述现有的控制添加,所以我想要的控制,以被从上到下排序1,2,3,4,5,6,它的订货它们作为6, 5,4,3,2,1从上到下

The problem I am experiencing is that new controls are added above the existing controls, so where I want the controls to be ordered 1, 2, 3, 4, 5, 6 from top to bottom, it's ordering them as 6, 5, 4, 3, 2, 1 from top to bottom.

我想知道我是如何保证现有的所有控件后的新控件添加(以显示顺序而言)。

I want to know how I ensure a new control is added after all existing controls (in terms of display order).

还有,我想知道我是否可以插入其他两个选定的控件之间的控制

And also, I want to know if I can insert a control between two other selected controls

我已经尝试设置的TabIndex,但没有帮助!

I have tried setting the TabIndex but that didn't help!

推荐答案

在使用的WinForms只在控件添加决定了他们的停靠行为序列。

When using Winforms only the sequence in which controls are added determines their docking behaviour.

最后添加的控件将总是最靠近对接边界,即用 DockStyle.Top 上方。 无论是 BringToFront SendToBack 或Tab键顺序将改变这一点。

The last added control will always go nearest to the docking border, i.e. to the top with DockStyle.Top. Neither BringToFront nor SendToBack or Tab-order will change this.

只需添加你的控件以相反的顺序,或删除并重新添加。

Just add your controls in reverse order, or remove them and add them again.