.NET Windows Forms - Can't Change Docking Order

By Robbe Morris

Having trouble adjusting the docking order of your controls in a .net windows forms application? Simply undocking and then redocking the controls in the order you want hardly ever works.

The only way I've found to consistantly get this to work is to undock all of the applicable controls from their container.  This could be all controls on a form or all controls on a given panel (or some other container control).

Then, dock the first control.  For all subsequent controls that need to be redocked, use CTRL-X to temporarily delete them from Visual Studio and then CTRL-C to paste them back in again.  Then, redock that control.  You'll have to do this for each control.  All of the control names and attributes will remain the same.

However, you will need to recreate the event wireups.  For most controls, this is as simple as double clicking the control or going to the events section of the property grid.

After you have done this, close the designer for the form and open the .cs code file.  If you have found that Visual Studio created new event place holders like this:

old

private void mybutton_onclick

new

private void mybutton_onclick_1

Remove the new instance and try to compile your app.  Visual Studio
should fail to compile and give you all the places you need to tweak in the designer.cs file.

What is important to note is that if you attempt to fix the event place holders while the form is still up in design mode, Visual Studio will remove all of the newly created placeholders and you'll be back to square one for recreating your event placeholders.

Popularity  (2661 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of EggHeadCafe.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP
Create New Account
Article Discussion: .NET Windows Forms - Can't Change Docking Order
Robbe Morris posted at Thursday, October 12, 2006 3:41 PM
reply
Cristian replied to Robbe Morris at Tuesday, December 26, 2006 8:51 PM
Actually there is a better and very easy way of doing this. The docking order is set using de z-index, which is internally incremented by one when you create a new control (or paste it) in the designer. But you can easily change this value right clicking the visual control and clicking "Bring To Front" or "Sent to Back". This will change the docking order.

So, if you want you controls to dock from up to down, just right-click them in that order and click "Bring To Front".
reply
In complex windows forms with dozens of controls on them
Robbe Morris replied to Cristian at Tuesday, December 26, 2006 8:51 PM
even your suggestion didn't work.  I'd suggest others try your suggestion first before resorting to mine.  That said, my tip came from the VS.NET 2005 days.  VS.NET 2010 may have resolved that.
reply
Cristian replied to Robbe Morris at Tuesday, December 26, 2006 8:51 PM
That makes sense. I'm using VS 2010, where it works for me.
reply