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

蓝咒 提交于 2019-12-11 02:55:32

问题


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 ?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/4335040/how-can-i-add-a-combobox-in-control-designer-properties-for-a-winforms-custom-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!