Thursday, May 22, 2008

Workaround: The SplitContainer may crash your VS2008 Designer

If use the default Split Container to build a Windows Form or create a new user control, and set some properties of that Split Container at design time using Visual Studio 2008, you may end up with a broken form or user control. I got that error using the Visual Studio 2008 - Professional Edition.

How to fix this error

First of all, if you encounter that error you will see a screen like this:


To fix this you have to locate the designer file of the affected form or user control. Search for the Split Container generated source code. It will look like this:

//

// splMain

//

this.splMain.Dock = System.Windows.Forms.DockStyle.Fill;

this.splMain.Location = new System.Drawing.Point(0, 0);

this.splMain.Name = "splMain";

//

// splMain.Panel1

//

this.splMain.Panel1.Controls.Add(this.ultraTabControl1);

this.splMain.Panel1MinSize = 300;

//

// splMain.Panel2

//

this.splMain.Panel2.Controls.Add(this.prgProperties);

this.splMain.Panel2MinSize = 250;

this.splMain.Size = new System.Drawing.Size(702, 339);

this.splMain.SplitterDistance = 448;

this.splMain.TabIndex = 0;



Now comment out the line where the Panel2MinSize property is set. It might be necessary to do the same for the SplitterDistance property. The resulted code should look now like this:

//

// splMain

//

this.splMain.Dock = System.Windows.Forms.DockStyle.Fill;

this.splMain.Location = new System.Drawing.Point(0, 0);

this.splMain.Name = "splMain";

//

// splMain.Panel1

//

this.splMain.Panel1.Controls.Add(this.ultraTabControl1);

this.splMain.Panel1MinSize = 300;

//

// splMain.Panel2

//

this.splMain.Panel2.Controls.Add(this.prgProperties);

//this.splMain.Panel2MinSize = 250; <--- This is the cause of the error!

this.splMain.Size = new System.Drawing.Size(702, 339);

this.splMain.SplitterDistance = 448;

this.splMain.TabIndex = 0;


Now try to open the form again!

kick it on DotNetKicks.com

1 comment:

LosManos said...

I have found a similar problem. Since I started naming my split containers explicitly I haven't run into the bug any more. Se here: http://www.selfelected.com/change-the-name-of-splitcontainer1/