开发者

How can I add a combobox in control designer properties for a WinForms custom control?

开发者 https://www.devze.com 2023-01-28 04:38 出处:网络
I\'m creating a custom control with a pro开发者_StackOverflowperty 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 th

I'm creating a custom control with a pro开发者_StackOverflowperty 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 ?


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; }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号