我怎么可以添加控件设计属性组合框一个WinForms自定义控件?控件、组合、自定义、属性

2023-09-07 09:33:03 作者:惊°世妖娆

我要创建一个属性,可以从一组像男人,女人的字符串值,取一个自定义的控制。因此,在控件设计性能我想告诉这些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 ?

推荐答案

最简单的方法来做到这一点是添加的枚举您的code定义可能的选择你的财产,然后配置你自定义的控件的属性来接受这种类型的值。的属性窗口将自动为这个属性与所有在枚举中列出的可能值显示组合框。

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.

所以,例如:

public enum Gender
{
    Man,
    Woman,
}

public class MyCustomControl : UserControl
{
    public Gender UserGender { get; set; }
}