Sign in to follow this  
Telamon

[.net] Form designer keeps deleting my control :-(

Recommended Posts

Telamon    157
I'm using C# with VS 2003 Enterprise Architect and I've got a user control that I wrote which displays a tilemap using DirectX (but it doesn't create a D3D device until runtime). I've got an instance of this control drawn on my main form. Every so often when I open the project file, I find that the IDE has deleted the user control from my form. Everything works if I redraw it back on but it's driving me crazy. Anyone had a problem like this before?

Share this post


Link to post
Share on other sites
Geronimo2000    270
I've had similar designer problems that were solved by closing and re-opening Visual Studio. Having the files under source control is also very helpful when the Form Designer is screwing you over, since having the file as read-only will prevent the problem from being saved on your hard drive.

Share this post


Link to post
Share on other sites
itachi    296
Quote:
Original post by _DarkWIng_
My co-worker had this problem with some user created controls. His "solution": Don't open form in design mode.


The .Net design time features are pretty cool, so totally avoiding the designer is not really the best solution imo. I've had the problem a couple of times, but I was always able to find the reason.

Sometimes the reason was that the Designer was trying to set Properties (public read/write properties are serialized by default) that weren't meant to be set at design time because certain resources (or whatever) are only available at run time.

In that case you could either check the Control.DesignTime Property (available after the Control has been loaded) and do things differently at design time.

If the Property should not be serialized at all, you can mark it with [DesignerSerializationVisiblity(DesignerSerializationVisibility.Hidden)].

Another option to control serialization is to add a bool property "ShouldSerializeMyProperty", where MyProperty is replaced with the property's name.

Checking the code paths of the Control is a good idea too (Controls in the Designer are created through their public default constructors). Finally, you can use an other instance of VS.Net to debug your design time code. See here.

One last thing, while you're working on the control's code, it's a very good idea to not keep any Forms that are using the Control open in the designer while compiling.

Share this post


Link to post
Share on other sites
jods    367
Quote:
Original post by itachi
In that case you could either check the Control.DesignTime Property (available after the Control has been loaded) and do things differently at design time.


One word of warning though: the DesignTime property does NOT work in the constructor (which makes sense: it is set by the designer AFTER the instance is created).

There are other tricks that work in the c'tor, but I don't remember them at the moment. Just googling a bit should bring them up.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this