10

I use panel.controls.add to add controls to the panel. But... they insert at the very top of it.

I tried the BringToFront and SendToBack methods, but does'nt seem to be working.

Any ideas? Thanks

EDIT:

what i want, is that they dock at the top of the container, but if there is another docked control there, the new one is displayed below that one...

saggio09
  • 177
  • 1
  • 2
  • 10
  • 1
    The title of your question confuses me somewhat. Either you want them docked at the top or at the bottom of the container control, which one is it? Use the `Dock` property in either case – Konrad Morawski Oct 07 '11 at 07:38
  • Insert at the very top of it means? You want new controls in bottom? – Anuraj Oct 07 '11 at 07:39
  • what i want, is that they dock at the top of the container, but if there is another docked control there, the new one is displayed below that one... – saggio09 Oct 07 '11 at 07:50

5 Answers5

24

The docking order is based on the index of the control in the Controls collection. The last one goes on top. Which is why your added control goes on top and pushes an existing docked control down. Use the SetChildIndex() method to move the control to index 0:

        var btn = new Button();
        btn.Dock = DockStyle.Top;
        panel1.Controls.Add(btn);
        panel1.Controls.SetChildIndex(btn, 0);
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

You have two ways of doing this.

  1. Add the controls in the order that you want them to be displayed in a panel, dock the controls to the bottom as you create them.

    Example:
    Panel.Controls.Add(Label1)
    Panel.Controls.Add(Label2)
    Panel.Controls.Add(Label3)
    
  2. Reverse the order of the controls being added to the panel, dock the controls to the top as you create them.

    Example
    Panel.Controls.Add(Label3)
    Panel.Controls.Add(Label2)
    Panel.Controls.Add(Label1)
    

If this is not what you want, you will want to reverse either the order of what is being added (which does not seem likely) or docking (up vs down).

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
RVK
  • 31
  • 1
1

"what i want, is that they dock at the top of the container, but if there is another docked control there, the new one is displayed below that one..."

OK I understand. I see two solutions:

  1. Use Anchor property (set to Top) instead of Dock property - then these controls will be placed at the top side by side, however they won't stretch horizontally, they won't automatically occupy all available horizontal space (you can still control their Width programmatically of course). If this limitation is a problem for you, try option 2:

  2. Use another container control - it could be a TableLayoutPanel - dock it at the top of the first panel, and then put the remaining controls in that TableLayoutPanel. Use its Columns collection to provide space for multiple controls next to eachother. (Embedding container controls in other container controls is not unusual and often necessary when designing complex layouts.)

Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91
0

How to rearrange layout panels on a form at Design time:

Open your form in Design view. Open the Document Outline window - View|Other Windows|Document Outline - (Ctrl+Alt+T).

The Document Outline shows the layout panels in a tree view in the reverse order that they are shown on the form. Within the Document Outline window you can rearrange the order of the panels. Due to the reverse order whatever is shown at the top in the Document Outline will be at the bottom of the form.

Keshan Nageswaran
  • 8,060
  • 3
  • 28
  • 45
DanW52
  • 67
  • 7
0

Use FlowLayoutPanel and add all the control in this panel.

To add newest contrtol below to the previous control

flowLayoutPanel1.FlowDirection = FlowDirection.LeftToRight;
flowLayoutPanel1.AutoScroll = true; 

Label label1 = new Label();
flowLayoutPanel1.Controls.Add(label1);
label1.SendToBack();
Naveen Verma
  • 367
  • 1
  • 5
  • 18