Adding/Docking controls in UserControl C# .NET

2019-07-20 19:16发布

问题:

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

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

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

回答1:

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

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.



回答2:

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


回答3:

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.



回答4:

I know this is years old but what the heck.

You can use the SetChildIndex method to control this as follows

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