简体   繁体   English

在UserControl C#.NET中添加/停靠控件

[英]Adding/Docking controls in UserControl C# .NET

I am writing a UserControl which adds child controls programmatically. 我正在编写一个UserControl,它以编程方式添加子控件。 Currently I am adding new controls like so: 目前,我正在添加新的控件,如下所示:

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

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. 我遇到的问题是,在现有控件的上方添加了新控件,因此我希望将控件从上到下按顺序排列为1、2、3、4、5、6,将它们按6、5、4排序,3、2、1从上到下。

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 而且,我想知道是否可以在其他两个选定的控件之间插入一个控件

I have tried setting the TabIndex but that didn't help! 我尝试设置TabIndex,但这没有帮助!

When using Winforms only the sequence in which controls are added determines their docking behaviour. 使用Winforms时,只有添加控件的顺序才能确定它们的对接行为。

The last added control will always go nearest to the docking border, ie to the top with DockStyle.Top . 最后添加的控件将始终最接近停靠边框,即使用DockStyle.Top到达顶部。 Neither BringToFront nor SendToBack or Tab-order will change this. BringToFrontSendToBack或Tab顺序都不会更改此设置。

Just add your controls in reverse order, or remove them and add them again. 只需以相反的顺序添加您的控件,或者将其删除并再次添加即可。

Here's my solution to this. 这是我的解决方案。 Basically you put the controls in a list as well as the container. 基本上,您将控件以及容器都放在列表中。 Then you use Bring to Front as mentioned is pretty much all posts. 然后,您可以使用前面提到的几乎所有帖子。 This of course also gives you the posibility of insert. 当然,这也使您可以插入。

    Panel control1 = new Panel() { Height = 16, Dock = DockStyle.Top, BackColor = Color.Red};
    this.Controls.Add(control1);
    Panel control2 = new Panel() { Height = 16, Dock = DockStyle.Top, BackColor = Color.White };
    this.Controls.Add(control2);
    Panel control3 = new Panel() { Height = 16, Dock = DockStyle.Top, BackColor = Color.Black };
    this.Controls.Add(control3);
    Panel control4 = new Panel() { Height = 16, Dock = DockStyle.Top, BackColor = Color.Yellow };
    this.Controls.Add(control4);
    Panel control5 = new Panel() { Height = 16, Dock = DockStyle.Top, BackColor = Color.Blue };
    this.Controls.Add(control5);
    Panel control6 = new Panel() { Height = 16, Dock = DockStyle.Top, BackColor = Color.Green };
    this.Controls.Add(control6);
    PanelList.Clear();
    PanelList.Add(control1);
    PanelList.Add(control2);
    PanelList.Add(control3);
    PanelList.Add(control4);
    PanelList.Add(control5);
    PanelList.Add(control6);
    Panel control7 = new Panel() { Height = 16, Dock = DockStyle.Top, BackColor = Color.Pink };
    this.Controls.Add(control7);
    PanelList.Insert(3, control7);
    for (int i = 0; i < PanelList.Count; i++)
    {
        PanelList[i].BringToFront();
    }
private Int32 m_OffsetY = 0;
private Int32 m_MarginY = 10;

private void AddControl(Control control)
{
    SuspendLayout();
    Controls.Add(control);
    control.Location = new Point(m_OffsetX, m_OffsetY);
    ResumeLayout();

    m_OffsetY += control.Height + m_MarginY;
}

// ...

For what concerns controls insertion... it's not possible as controls position depends on the order they are added to the form. 关于控件插入的问题……这是不可能的,因为控件的位置取决于它们添加到表单的顺序。 If you have layout space, however, you can insert a control between two controls physically... you calculate ctrl1 and ctrl2 positions and dimensions and you set the location of the new one depending on this. 但是,如果有布局空间,则可以物理上在两个控件之间插入一个控件...您可以计算ctrl1和ctrl2的位置和尺寸,并据此设置新控件的位置。

I know this is years old but what the heck. 我知道这已经几岁了,但是到底。

You can use the SetChildIndex method to control this as follows 您可以使用SetChildIndex方法来控制此操作,如下所示

var someControl = new UserControl();
someControl.Dock = DockStyle.Top;
MainForm.Controls.Add(someControl);
MainForm.Controls.SetChildIndex(someControl, 0);

Source: http://tipsntricksbd.blogspot.com/2009/10/c-dynamically-adding-control-with.html 来源: http//tipsntricksbd.blogspot.com/2009/10/c-dynamically-adding-control-with.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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