简体   繁体   English

如何在WinForms自定义控件的控件设计器属性中添加组合框?

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

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 ? 如果没有,我应该执行什么?

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. 然后将您的属性设置为Person。 It should appear in properties as a drop down list. 它应该在属性中显示为下拉列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM