views:

826

answers:

3

I have a somewhat complex UserControl, and Visual Studio 2008 is giving me a rather harmless annoyance when working with it. Every single time I open the control with the Designer, it decides to immediately change some of the harmless values set by the designer - namely the initialization of Size properties. If I save those changes, close, and reopen, it almost invariably ends up deciding another component of my control needs its initial size changed, ad infinitum. Luckily these changes are harmless since I'm using automatic sizing everywhere, but this is quite annoying to work with. I haven't the foggiest on where to start figuring out what's going wrong, my only thought right now is that the Designer is assigning the results of auto-sizing back into the initial size fields every time I open the control. Any ideas on causes/fixes?

Edit: Also, I am using Application Settings to save sizes of certain resizable child components across runs of the application, but I really hope the Designer is smart enough to understand that it should only ever be using the defaults.

+1  A: 

You're right, the designer often tries to add default values to properties.

Add this on top of the property declaration:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

That will tell the designer to ignore this property.

jean
I can't do that with the Size property, as it is inherited from Control and non-virtual (well, without redeclaring and piping to the base class, which is silly). Also, I'm using mostly out-of-the-box components, so I'd have to change all these to my own custom derived classes, not really a good option either.
Not Sure
I don't see how you can take control of the Size property without "newing" it.
Benjol
A: 

I have somewhat similar problem. I am using Infragistics GroupBox on a user control which I inherited and now want to change its look and feel in the derived class. I have made it protected in base class -- so it does allow me changing properties in derived class. But it does not save it. Every time I open it -- I get same old values of base class back.

Any idea?

Edit: I figured it out.

Trying various value for one of the above given answers. Using [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] instead of [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] generates code for changed properties - and things work as desired.

A: 

Try overriding the DefaultSize property of your control.

From MSDN: The DefaultSize property represents the Size of the control when it is initially created.

Rodney Richardson
It's not my control whose Size is changing, it's all the child controls whose Sizes are getting changed.
Not Sure