saggio09 saggio09 - 26 days ago 7
Vb.net Question

How to insert top docked controls below previewsly top docked controls on a panel

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...

Answer

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