Amol
Amol

Reputation: 133

How can I hide a property from going into the form designer file?

I am working in VB.NET 2010 Framework 2.0.

I don't want to allow some properties from going into form's designer file but the those properties will present on the form(property grid). The behavior of these properties will be same always.

I used the following code:

<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Property GradientBegin() As Color = Color.Red 

But the problem I am facing is that -> on the property grid after changing the "GradientBegin" color to other than RED and compiling the program, it is replacing the new changed value to RED again. So I am not able to change the color actually.

How can I achieve this?

Thanks for any reply in advance.

Upvotes: 0

Views: 2915

Answers (2)

jnm2
jnm2

Reputation: 8364

If you apply <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>, any change to that property on the property grid will not be saved. The only way you can save changes made in the designer is if you do allow it to be put in the designer file.

If you don't want the designer to save that property from the property grid, you'll need to use code to set the property.

If your problem is that you are setting the property from the code but it is changing back to red anyway, it's because the designer serialized it before you applied the attribute and it's still in the designer file. In that case you need to go into the ClassName.designer.vb file and delete the leftover line that sets the color to be red. It won't reappear as long as out have that DesignerSerializationVisibility attribute.

Upvotes: 0

Cody Gray
Cody Gray

Reputation: 244981

As best I can tell, you're asking for two completely contradictory things.

Setting the DesignerSerializationVisibility attribute to "Hidden" will prevent the designer from saving any information about how you set those properties. Their default values will always be used, because no custom settings are stored.

Therefore, when you change the GradientBegin property to be a color other than Red, that setting is not getting saved into your form's designer file. That's why no matter what happens, when you compile and run your program, the GradientBegin property is changed back to its default value of Color.Red. (And just in case you're not sure where that default is coming from, it's the value you set when you declared the property: Public Property GradientBegin() As Color = Color.Red.)

So, you have to decide which behavior you actually want. Do you want the designer to save (serialize) however you set the property at design-time? If so, you need to remove the DesignerSerializationVisibility attribute from your property's declaration. Otherwise, if you want to leave that attribute, you'll have to stick with whatever the default value is for that property, because that's the only thing your program will know when it is run. The only other option is to set the GradientBegin property explicitly in your code, rather than leaving the designer to do it. This means you can't set it using the Properties Window at design-time, but it does allow you to leave both the DesignerSerializationVisibility attribute as well as override the default value for the property.

Upvotes: 1

Related Questions