4

I know when I use Enum and put it as Property for class/UserControl, it will show/display it with drop down list like this:

enter image description here

But is this the only way to display available values like "drop down list"? Do we have other ways to do it?

Edit: The scope is Windows Form Application

For who didn't understand me

In that image(https://i.stack.imgur.com/NMank.png) where wrote "DataBase" is the Name of the Property in design time.

The values: NotChoseYET, ChooseDataBase, ThereAlreadyDataBase are values in Enum I created.

As you can see they displayed in a DropDownList in the design, so I want to ask if there are other ways to display like that without Enum?

nevets
  • 4,631
  • 24
  • 40
  • You can use a converter that will take the enum value and return a "displayable" string. – d.moncada Aug 26 '14 at 21:15
  • what??? i misunderstand you my question isn't convert any Enum to anything. I just wonder if there any control or any other way to show values like in the picture Except from using Enum – onemore More Aug 26 '14 at 21:18
  • What kind of project is this? WinForms, WPF, etc? – Joshua Shearer Aug 26 '14 at 21:23
  • Windows Form Application – onemore More Aug 26 '14 at 21:23
  • I don't really understand your question. Is that the only way to *automatically* do it? Probably. You can use any control you want though (Radio buttons come to mind), you'll just have to set some of it up yourself. Or is this due to it being in a DataGrid (not clear if it is from your picture)? – BradleyDotNET Aug 26 '14 at 21:25
  • @BradleyDotNET i ask if there other way to values like "DropDownList" in the designer/Property or only Enum can do it if i use Enum and make values in that enum, when i make Property to this Enum, it display in "DropDownList". so i ask if there other way to display values in "DropDownList" in designer without Enum – onemore More Aug 26 '14 at 21:27
  • Are you asking if there is another control which displays values like `DropDownList` but isn't a `DropDownList`? – Michael McGriff Aug 26 '14 at 22:14

3 Answers3

2

Update

After tons of searches, I found what I had answered was only partially correct, so I would like to answer it again.

The answer to your question is a definite NO, we have some other way to show values on property as DropDownList. Like what I mentioned in my old answer, if values come from some kind of Set, or in other word it belongs to a collection of values, it will be displayed as a DropDownList without any extra efforts (because the UITypeEditor has been specified for them, you will understand this point later). Here are 3 examples:

  • If a Property is a bool, in the designer it will show you a DropDownList contains True and False;

drop down list with 2 values

  • If a Property is a Font.Name, in the designer it will show you a DropDownList with SmallIcon;

drop down list with small icon

  • if a Property is a Color, in the designer it will show you some DropDownList encapsulated in a TabControl.

tab control encapsulates drop down list

From those "native" examples, you may realize a fact: we could somehow use some controls other than a simple DropDownList in the Property Tab during the design time. For example, a Color property gives a TabControl instead of a simple DropDownList, and a Font.Name property gives a customized DropDownList.

Yes, this is the second thing I am going to talk about. You are not only able to customize the items in the DropDownList, but also the View of that Value Choosing Editor.

However, this is very tricky and non-trivial. You are not recommended to do this unless it really adds value to your user control in the design time.

In short, you need to inherit from UITypeEditor, and override 2 functions:

GetEditStyle(), which

indicates to the design environment which kind of user interface your UI type editor implements

and EditValue(), which

establishes the interaction between the design environment and the user interface for editing your custom type

Then for your property which makes use of the Editor, you need to add EditorAttribute to it to specify the Editor used when selecting value of that property.

For better details, you are suggested to check this MSDN walk-through, which explains how to design a customized Value Editor in the design time.

Now back to the point where we left over. The native type, like bool and Color, has already bond to a UITypeEditor, thus no further working should be done.


Old Answer:

For properties, you need to choose a value from a kind of Set, and all elements in that Set will be displayed as Items in a DropDownList during design time. When you try to define the Set, Enum is one method to define them. Also, you can use set of struct, like Color. In other words, if you have a Property that returns Color (or other structs), during design time it will appear as a drop down list.

nevets
  • 4,631
  • 24
  • 40
0

You can easily add items to a combobox control by using its .Items property:

  private void TestForm_Load(object sender, EventArgs e)
  {
     for (int i = 0; i < 10; i++)
     {
        // add items to the combobox
        // you can use any object. The .ToString() method will be used for displaying it
        cbxTest.Items.Add("This is test string " + i);
     }
  }

This will yield the following form:

enter image description here

WeSt
  • 2,628
  • 5
  • 22
  • 37
  • i didnt mean in run-time, i mean in designer mode, when you choose property to control such as :font,back Color. if there way to display properties like that (as you show) but in Designer, show it as properties – onemore More Aug 26 '14 at 21:30
  • 1
    You can still use the .Items property then. Even in the designer. – WeSt Aug 26 '14 at 21:31
  • i know that, but i didn't mean that. look at that image: http://i.stack.imgur.com/NMank.png that one property of control i made. that open it in the design mode like combobox that property is Enum – onemore More Aug 26 '14 at 21:35
  • in that image(http://i.stack.imgur.com/NMank.png) where wrote "DataBase" is the Name of the Property and the values: "NotChoseYET,ChooseDataBase,ThereAlreadyDataBase" are values in Enum i created. you see in the design it show like DropDownList so i ask if there other way to display like that without Enum – onemore More Aug 26 '14 at 21:37
  • @WeSt he wants it to be done in the design time but not the run time – nevets Aug 27 '14 at 15:19
0

I came across this question as I recently needed to do the same. I posted a question ... which in the end I anwsered myself here.

In a nutshell: Implement a type converter GetStandardValuesSupported(ITypeDescriptorContext context) to return true and GetStandardValues(ITypeDescriptorContext context) to return the StandardValuesCollection for the property type. Finally just decorate the property like so:

[TypeConverter(typeof(MyClassConverter))]
public MyClass MyProperty { get; set; } 

The designer property window will now have a dropdown with valid values.

okipol
  • 1,197
  • 3
  • 11
  • 27