Janik Zikovsky
Janik Zikovsky

Reputation: 3256

Visual Studio Property Grid: Accept an empty string for Int properties

I have a property grid with various value types including integers. Some of the int properties have the DefaultValueAttribute, specifying a default value of, e.g. -1.

If a user enters an empty string, Visual Studio complains that "Property value is not valid; '' cannot be converted to Int32". Is there anyway to interpret an empty string as a reverting to the default value?

We have a custom PropertyDescriptor implemented, but the SetValue() method is called after VS does the conversion to the object value type.

Upvotes: 0

Views: 544

Answers (1)

Nicolas Cadilhac
Nicolas Cadilhac

Reputation: 4745

Create a derived class of Int32Converter and attach it to your property. Override its ConvertFrom method to insert the behavior you want. Optimally you will use the DefaultAttribute set to the PropertyDescriptor to make it reusable.

Use the Reflector tool in case you want to see how BaseNumberConverter.ConvertFrom is done.

Upvotes: 1

Related Questions