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

Started by
4 comments, last by DrGUI 18 years, 9 months ago
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?

Shedletsky's Bits: A Blog | ROBLOX | Twitter
Time held me green and dying
Though I sang in my chains like the sea...

Advertisement
My co-worker had this problem with some user created controls. His "solution": Don't open form in design mode.
You should never let your fears become the boundaries of your dreams.
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.
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.
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.
I think that happens if you mess with its automatically generated code.

This topic is closed to new replies.

Advertisement