Search code examples
c#devexpressdesigner

Reference a class constant in C# Designer?


I am stuck using a designer for this current project I am working on. One thing that we commonly do is set as many properties as we can on each control we add. We do this using the designer "Properties" window. This causes all those settings to be set/managed in the generated .designer.cs file instead of us having to maintain them with our code.

There is one thing I find myself always wanting to do, but I can never figure out how to do it, and that is to refer to constants from the properties window.

For example, in my SpinEdit that I am working with, I would like to set the maximum value to be Decimal.MaxValue but if I just type that in the properties window, then I get an error. How do I reference this variable from the designer properties window?

Here is a picture of what I am trying to do, in case that makes it more clear:

Set property using constant

However attempting this causes and error as soon as I try to leave the focus of that text box highlighted in the picture. But it works fine in code, the following line compiles just fine, so why can't I do this in the designer?:

alarmStationSpinEdit.Properties.MaxValue = Decimal.MaxValue;

SpinEdit is provided by the DevExpress 3rd party library. I don't think the component is important though. I think the same problem applies to any field in the properties window.

I am expecting one of two possible answers. They would be in the form of either:

  1. Yes, that is possible. This is how you do it.....

or

  1. No, that is not possible. This is why..... (preferably with links since I find this hard to believe).

Solution

  • 1) Yes, that is possible if you are the component developer. Visual Studio property grid allows you to specify a custom value converter to a component property. This gives an opportunity to convert a string value entered in the property grid to a value read from the constant.

    The TypeConverterAttribute attribute is used to assign a custom type converter to a property. This is a detailed MSDN article with examples that explains what the type converter is and for what it can be utilized: Implement a Type Converter

    2) No, that is not possible if you are using a third party component and this component does not support assigning constant values to its properties.

    By default Visual Studio property grid accepts values as is. If the property is of the decimal type, you cannot assign a string to it.

    The value you assign via the property grid will not be evaluated as a C# or VB.NET expression. Visual Studio treats it as a value.