views:

407

answers:

2

I need to dock a WinForms user control at run time but I'm encountering an issue.

In my main form, I have a menu strip at the top of it. I want to programmatically add a UserControl docked to the left of the main window and below the menu strip.

When I add my User control at runtime and then set its Dock property to DockStyle.Left, it does not appear below the menustrip but instead it takes up the entire left side of the form. This causes the menustrip to be pushed to the right and does not look attractive.

I need to do this at runtime because the project requires the ability to customize which user control can appear on the left side of the main form for each user.

Any help in finding an answer would be greatly appreciated.

+1  A: 

A quick and dirty way to do it would be to add a Panel beneath the MenuStrip (docked Left or Full), then add the UserControl to the Panel.

Edit: What's happening is that the order in which controls are added to a container determine how they will appear on the form. Since you're doing this programmatically at some point after the form has been created, the UserControl is the last Control added to the Form, so it's going to affect those items already on the form. If you have a Panel on the Form that makes things appear as you want them too, adding the UserControl to the Panel will only affect those items inside the Panel and not on the rest of the Form.

Michael Todd
Great explanation! Thanks!
Stephen Fletcher
A: 

Add an Panel which docks below the toolstrip, and add the User Control to that container.

Edit: Already answered.

Dykam