How do I add dynamically generated Windows Forms controls?

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

I have a panel and although I can do pnlMyPanel.Controls.Add (ctl) ... it always inserts it at position 0 when I would rather add it to the end or pnlMyPanel.Controls. graph.

Am I missing the method, or will I have to do something else?

+2


source to share


2 answers


It depends on how your controls are laid out.

I am assuming that all the controls on the panel have their own set of properties Dock

. If so, call BringToFront

, SendToBack

or SetChildIndex

a new control after adding it to the panel.



If not, set the properties Top

and Left

/ or property of the Location

new control.

+1


source


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

you can use

SetChildIndex method



to change the order of the child control after adding the child control.

When SetChildIndex is called, the Control pointed to by the child parameter is moved to the position specified by newIndex and other Control references in Control .. ::. ControlCollection: reordered for movement. control with index zero at the top of the z-order and above the digit closer to the bottom.

+1


source







All Articles