Is it possible to attach a List of strings to a String property so that the user can select one of the strings from the Properties window? Should I implement ICollection or something of that sort?
Asked
Active
Viewed 2,821 times
0
-
One more question: Is there any way for me to pass an argument to the TypeConverter constructor when using it as an attribute? I want to be able to use this attribute to be able to create a string drop-down list property in any class using a List
provided by the user..so it doesn't work to just set a default list of Strings in the TypeConverter class. – alexD Sep 28 '09 at 23:27 -
You should mark something as an answer here, then ask a new question, since this is opening a new topic. – Reed Copsey Sep 28 '09 at 23:41
2 Answers
4
If you are trying to restrict a property to one of a few specific options, you should use an Enum instead of a String for the property.
If you want to provide defaults, but let them type any string in and ignore the defaults, then you can use StringConverter. For details, read Getting the Most Out of the .NET Property Grid control. It covers this exact scenario.

Reed Copsey
- 554,122
- 78
- 1,158
- 1,373
-
I checked that link out but it appears that it tells you how to do it for a PropertyGrid object - I can't get this to work in my design-time Properties window. Everything I've found seems to be for a PropertyGrid control - is there any way to do this at design time? – alexD Sep 28 '09 at 17:38
-
That's the same thing. The design time properties windows IS a PropertyGrid control - if you do what it's describing, it will display that way at design time. – Reed Copsey Sep 28 '09 at 17:40
-
Thank you this worked...I accidentally had the wrong type in my Attribute (had StringConverter but my class name was ColumnStringConverter, so it wasn't working). – alexD Sep 28 '09 at 22:45
-
It would be nice for once if people actually answered the question instead of telling us how it should be done. Having the property as an enum instead of a string isn't always going to work. Take the HyperLink class' Target property for example. It is (necessarily) a string, but in design mode it has a pre-defined list of options, e.g. "_blank", "_parent" etc. – Jan 09 '11 at 14:42
4
No. You should create an enum
type with your string choices, and make the property of that type. Example:
public enum Choices
{
NiceChoice,
PoorChoice
}
public class Chooser
{
public Choices Choice { get; set; }
}

Tomas Aschan
- 58,548
- 56
- 243
- 402
-
I can't use an enum because the list is dynamically generated. Basically when the user sets the datasource for a DataGridView, the property is going to read the column names and populate the list with those names. The user will then be able to select which column they want to filter. – alexD Sep 28 '09 at 17:39