这一节我们共同学习下用户控件的下拉式属性设置可以为用户控件设置属性以下拉框的形式显示出来效果图如下
这里我们定义一个用户控件设置一个属性DropDownPropery
代码如下
public partial class UCLab : UserControl { public UCLab() { InitializeComponent(); } private string dropdownPropery=Hello; [Description(下拉属性)] [Editor(typeof(DropTypeDialogEditor)typeof(UITypeEditor))] public string DropDownPropery { get { return dropdownPropery; } set { dropdownPropery = value; } } }
接着我们该属性设置一个下拉式编辑器代码如下
/// <summary> /// 下拉式编辑器 /// </summary> public class DropTypeDialogEditor:UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { if (context!=null&&contextInstance!=null) { return UITypeEditorEditStyleDropDown;//显示下拉箭头 } return baseGetEditStyle(context); } public override object EditValue(ITypeDescriptorContext context IServiceProvider provider object value) { SystemWindowsFormsDesignIWindowsFormsEditorService editorService = null; if (context!=null&&contextInstance!=null&&provider!=null) { editorService = (SystemWindowsFormsDesignIWindowsFormsEditorService)providerGetService(typeof(SystemWindowsFormsDesignIWindowsFormsEditorService)); if (editorService!=null) { UCLab lab =(UCLab)contextInstance; UCDrop drop = new UCDrop(labDropDownPropery); editorServiceDropDownControl(drop); value = dropResult; return value; } } //return baseEditValue(context provider value); return value; } }
以上代码就可以实现当我们把用户控件拖动到窗体上设置该属性就可以以下拉式窗体显示