views:

195

answers:

2

While I can easily accomplish in ASP.NET using AddAt(), I am trying to do the same thing in Windows Forms.

I have a panel, and while I can do a pnlMyPanel.Controls.Add(ctl) ... it always inserts it in the 0 position, when I would rather have it appended to the end, or pnlMyPanel.Controls.Count.

Am I overlooking a method or am I going to have to do something else?

+1  A: 

It depends how your controls are being laid out.

I assume that all of the controls in the panel have their Dock property set. If so, call BringToFront, SendToBack, or SetChildIndex on the new control after adding it to the panel.

If not, set the Top and Left properties (or the Location property) of the new control.

SLaks
That is correct ... I am docking to the top.
mattruma
Then you want to call `BringToFront` after adding the control.
SLaks
The BringToFront worked perfectly!
mattruma
+1  A: 

AddAt functionality can be implemented by a combination of Add and SetChildIndex methods.

You can use

SetChildIndex Method

to reorder the index of the child control after adding the child control.

When SetChildIndex is called, the Control referred to by the child parameter is moved to the position specified by newIndex and the other Control references in the Control..::.ControlCollection are reordered to accommodate the move. The control with an index value of zero is at the top of the z-order, and higher numbers are closer to the bottom.

rahul