.net WinForm用户控件开发--(7)用户控件下拉式属性设置
这一节我们共同学习下用户控件的下拉式属性设置,可以为用户控件设置属性以下拉框的形式显示出来,效果图如下
这里我们定义一个用户控件,设置一个属性dropdownpropery
代码如下
[csharp]
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;
}
}
}
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;
}
}
}
接着我们该属性设置一个下拉式编辑器,代码如下
[csharp]
/// <summary>
/// 下拉式编辑器
/// </summary>
public class droptypedialogeditor:uitypeeditor
{
public override uitypeeditoreditstyle geteditstyle(itypedescriptorcontext context)
{
if (context!=null&&context.instance!=null)
{
return uitypeeditoreditstyle.dropdown;//显示下拉箭头
}
return base.geteditstyle(context);
}
public override object editvalue(itypedescriptorcontext context, iserviceprovider provider, object value)
{
system.windows.forms.design.iwindowsformseditorservice editorservice = null;
if (context!=null&&context.instance!=null&&provider!=null)
{
editorservice = (system.windows.forms.design.iwindowsformseditorservice)provider.getservice(typeof(system.windows.forms.design.iwindowsformseditorservice));
if (editorservice!=null)
{
uclab lab =(uclab)context.instance;
ucdrop drop = new ucdrop(lab.dropdownpropery);
editorservice.dropdowncontrol(drop);
value = drop.result;
return value;
}
}
//return base.editvalue(context, provider, value);
return value;
}
}
/// <summary>
/// 下拉式编辑器
/// </summary>
public class droptypedialogeditor:uitypeeditor
{
public override uitypeeditoreditstyle geteditstyle(itypedescriptorcontext context)
{
if (context!=null&&context.instance!=null)
{
return uitypeeditoreditstyle.dropdown;//显示下拉箭头
}
return base.geteditstyle(context);
}
public override object editvalue(itypedescriptorcontext context, iserviceprovider provider, object value)
{
system.windows.forms.design.iwindowsformseditorservice editorservice = null;
if (context!=null&&context.instance!=null&&provider!=null)
{
editorservice = (system.windows.forms.design.iwindowsformseditorservice)provider.getservice(typeof(system.windows.forms.design.iwindowsformseditorservice));
if (editorservice!=null)
{
uclab lab =(uclab)context.instance;
ucdrop drop = new ucdrop(lab.dropdownpropery);
editorservice.dropdowncontrol(drop);
value = drop.result;
return value;
}
}
//return base.editvalue(context, provider, value);
return value;
}
}
以上代码就可以实现,当我们把用户控件拖动到窗体上,设置该属性,就可以以下拉式窗体显示。
推荐阅读
-
.net WinForm用户控件开发--(7)用户控件下拉式属性设置
-
.net WinForm用户控件开发--(5)用户控件复杂属性设置
-
.net WinForm用户控件开发--(1)带按钮的textbox控件
-
.net WinForm用户控件开发--(3)可多选的下拉列表框
-
.net WinForm用户控件开发--(2)带按钮DataGridView
-
.net WinForm用户控件开发--(5)用户控件复杂属性设置
-
.net WinForm用户控件开发--(7)用户控件下拉式属性设置
-
.net WinForm用户控件开发--(1)带按钮的textbox控件
-
.net WinForm用户控件开发--(3)可多选的下拉列表框
-
.net WinForm用户控件开发--(2)带按钮DataGridView