2

I'm creating a custom control with a property that can take value from a set of strings like "Man, Woman". So in in control designer properties I want to show a combobox with these 2 choices.

Is there a standard way to do so ? If not what should I implement ?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
user310291
  • 36,946
  • 82
  • 271
  • 487

2 Answers2

6

The simple way to do that is to add an enum to your code that defines the possible choices for your property, then configure your custom control's property to accept a value of that type. The Properties Window will automatically display a combo box for this property with all of the possible values in your enum listed.

So, for example:

public enum Gender
{
    Man,
    Woman,
}

public class MyCustomControl : UserControl
{
    public Gender UserGender { get; set; }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 2
    How do you do it, without a enum? I want to be able to get a list of all the controls on a form or if I have a control get all the child controls in it and let you the user select one of them from a dropdown menu in a property grid. – tobeypeters Dec 08 '18 at 14:40
  • @tobeypeters Did you ever found out how to do it ? I am facing the same requirement – GuidoG Jan 20 '21 at 15:32
3

As far as I remember, you should create an enum like:

enum Person
{
    Man,
    Woman
}

and then make your property of type Person. It should appear in properties as a drop down list.

Andrei Pana
  • 4,484
  • 1
  • 26
  • 27