3

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!

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313

4 Answers4

3

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.

888
  • 3,246
  • 8
  • 41
  • 60
Stephan Keller
  • 1,623
  • 11
  • 13
  • 1
    Thats odd, my panels do change order when I use BringToFront. I guess my VS2010 is magic :D – WozzeC Jan 22 '13 at 11:05
  • No. I just checked your solution and it works for me. So I'm proven wrong. I was pretty sure it wouldn't work because I spent some hours with nearly the same problem. – Stephan Keller Jan 22 '13 at 11:12
1

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();
    }
WozzeC
  • 2,630
  • 1
  • 13
  • 12
0
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.

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • I don't want the controls docked to the bottom, I want them docked to the top of the parent control, but in the order they are generated. Currently the last generated control always goes to the top, above previously generated controls – Matthew Layton Jan 22 '13 at 10:37
  • So... take an offset of the last added control and then set the location of the brand new added ones depending on that offset. For example... Offset = 0 -> Add Control -> Offset = 16 -> Add Control and Move Down at 16 -> Offset = 32 -> Add Control and Move Down at 32... – Tommaso Belluzzo Jan 22 '13 at 10:38
0

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

Josh
  • 1,032
  • 2
  • 12
  • 24