VisualHint's blog

The best way to attach a unit to a numerical value in the PropertyGrid

When you expect your end-user to enter a numerical value in a given unit, you usually want to make this information available near the editor. Entering a value in millimeter or centimeter makes a difference and it is mandatory to clear the ambiguity. In the Microsoft PropertyGrid or in SPG, it could be done like this:

  • Write this information in the comments area at the bottom of the grid. But what if you don't want to display this area?
  • Put it in the property label: "Frequency (in Hz)". Not very friendly since usually you like to read the unit after the value.
  • Use a derived TypeConverter so that you can have the value and its unit in the textbox. But in this case you are obliged to write the converter and you make the user think he can edit the unit.

The next release of SPG simplifies all this by extending the FeelEditUnit inplace control. You just pass the unit to the second value of the property (yes, SPG handles multiple value types per property). It can be passed as a string or as a collection containing only a single element).

When the property is not selected, you see this:

When it is selected and edited, you see a nice readonly unit, still in the value column:

As usual, the unit can be targetted in an instance or added on the fly with a call to AddManagedValue.