Friday, March 31, 2006 2:55 AM Christopher

Visual Studio 2005 Designer: Touchy, Thouchy!

If you do anything "out of the ordinary" in your UserControl or Form derived classes in Visual Studio 2005, let me introduce you to a little snippet that will probably save you lots of headaches:

if ( DesignMode ) return;

or, its equally useful counterpart

if(!DesignMode){
	doStuff();
}

Now, the "out of the ordinary" thing I was doing was...hooking up an instance EventHandler using a static property that exposes a Form derived class in an OnLoad override. What? I know, not the clearest situation, and probably one that could use a boatload of refactoring, but it works. In short, if you are having designer problems in Visual Studio 2005, it may be worth it to sit back for a bit and think about what introduced the designer error. Prime candidates are constructors and OnLoad overrides that use static methods for functionality. This code didn't allow me to view the designer of a different form:

public override void OnLoad(EventArgs e){
	AppManager.ConcreteEditorForm.NewPostCreated += new EventHandler(HandleNewPost);
}

while this one would:

public override void OnLoad(EventArgs e){
	if( DesignMode ) return;
	AppManager...
}
Filed under: ,

Comments

# re: Visual Studio 2005 Designer: Touchy, Thouchy!

Friday, March 31, 2006 4:45 AM by Tarek Hannoudi

Thanks for the tip!

I had the same problem.. I even did comment some code at design time and and uncommented it back for the compile ;-)

# re: Visual Studio 2005 Designer: Touchy, Thouchy!

Friday, March 31, 2006 10:16 AM by Marc LaFleur

Wow, great post. This just solved a long standing problem I've had with a use control.

# re: Visual Studio 2005 Designer: Touchy, Thouchy!

Tuesday, April 18, 2006 1:47 PM by Bob M

Excellent post.

My Winforms UserControl used a static constructor and a few static methods to initialize some data structures common to all instances of the control.

I would always get the classic "One or more errors encountered while loading the designer...." error message the first time I opened up any form that used the UserControl in the designer. It was maddening because after a single execution of the program the designer problems would disappear.

After reading this post I got rid of all static methods and the static constructor from my UserControl and like magic my designer problems were fixed!

Thanks. Some of this VS designer stuff borderlines on black magic.

# re: Visual Studio 2005 Designer: Touchy, Thouchy!

Tuesday, October 2, 2007 4:34 AM by Nathan

Dude, THANK-YOU. First Google response, great example. (If interested, I searched for "visual studio 2005 form design code"). Thank-you.

# re: Visual Studio 2005 Designer: Touchy, Thouchy!

Thursday, June 12, 2008 10:13 AM by gstoy

not always true

I've seen others mention that a control is not fully sited (i.e. ISite hasn't initialised itself completely) while its constructor executes, so that Designtime returns false even when in designer... The secret exit seems to be somewhere else