Question

I am finding that Design View (DV) is nice, but trying to change the way it automagically injects code into InitializeComponent is very hard. And sometimes its automagical code breaks the program.

For example, DV automatically sees every single property of my custom UserControl, and then it assigns every single property to some value in InitializeComponent. But I don't want it to assign values to some of the properties because some of my setters will throw a runtime exception if not used correctly. I could correct InitializeComponent manually, but anytime I make a change to the design, SharpDevelop will just regenerate the function again.

And there's another case where I have the Default Constructor set the size based upon certain factors, but then InitializeComponent will immediately set it to another static value.

How can I tell DV to not automagically assign values to certain properties I define?

Was it helpful?

Solution

Unless I misunderstood your scenario, it seems like you're barking up the wrong tree by trying to modify or reconfigure SharpDevelop's behavior. Even if you manage to change it, you won't affect Visual Studio's behavior, and you won't help any of the other consumers of your custom control who don't happen to (and/or don't want to) configure their designer accordingly.

Instead, it seems that you should just mark the properties exposed by your custom control with the [DesignerSerializationVisibility] attribute. This indicates to the designer exactly how that property's value should be serialized into the InitializeComponent method.

You have a choice of three different values:

  • Visible indicates that the value for the property should be persisted in initialization code
  • Hidden indicates that the value for the property should not be persisted in initialization code
  • Content indicates that initialization code should be generated for each public (not hidden) property of the object assigned to the property

The default value is Visible, which causes a property's value to be serialized whenever possible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top