我想把控件所在页面的所有的dropdownlist控件都遍历出来,然后用户直接选择就可以了。这需要写一个设计器用的类,派生自uitypeeditor
using system;
using system.drawing.design;
using system.web.ui;
using system.windows.forms;
using system.windows.forms.design;namespace mvcapp.webforms.controls
{
public class iteratepagecontrolsuitypeeditor : uitypeeditor
{
public override uitypeeditoreditstyle geteditstyle(system.componentmodel.itypedescriptorcontext context)
{
return uitypeeditoreditstyle.dropdown;
}public override object editvalue(system.componentmodel.itypedescriptorcontext context, iserviceprovider provider, object value)
{
iwindowsformseditorservice service = provider.getservice(typeof(iwindowsformseditorservice)) as iwindowsformseditorservice;
listbox lbvalues = new listbox();
lbvalues.borderstyle = borderstyle.none;
iteratepagedropdownlist(lbvalues, context, value);
lbvalues.click += (sender, arg) =>
{
service.closedropdown();
};
service.dropdowncontrol(lbvalues);
if (lbvalues.selecteditem != null)
return lbvalues.selecteditem.tostring();
else
return "";
}private void iteratepagedropdownlist(listbox lbvalues, system.componentmodel.itypedescriptorcontext context, object value)
{
system.web.ui.webcontrols.webcontrol self = context.instance as system.web.ui.webcontrols.webcontrol;
page page=null;
if (self.site.container.components.count > 0)
page = self.site.container.components[0] as page;
int currentindex = -1;
foreach (system.web.ui.control item in page.controls)
{
if (item is system.web.ui.webcontrols.dropdownlist == false || self.id == item.id)
continue;
currentindex = lbvalues.items.add(item.id);
if (item.id == value.tostring())
lbvalues.selectedindex = currentindex;
}
}
}
}
使用方法
[editor(typeof(iteratepagecontrolsuitypeeditor), typeof(system.drawing.design.uitypeeditor))]
public string clientdropdownlistid
{
get { return m_clientdropdownlistid; }
set { m_clientdropdownlistid = value; }
}
看一个自定控件实现代码
要实现这样的效果,我们只需要做2件事情。
1. 在控件类库中,添加一个windowform类,然后编辑相应的界面,并初始化
因为默认情况下 控件在designer.cs中定义为private,所以我们将他改为public。
2. 定制自己的uitypeeditor类。
2. 属性下拉编辑器.
1.在classlibrary中,新建一个usercontrol(categorydorpdown.cs).
2.实现自定义uitypeeditor.
3.因为windowform里的控件在designer.cs声明为private,如果声明为public,则在自定义uitypeeditor中引用不到控件,所以我们需要
combobox obj = dropform.controls[2] as combobox;需要找到下拉控件 并根据value 参数赋值,但我们要判断是否控件为null和是否为"".
赋值为obj.text=value as string.
3.
在这个自定义属性中,我继承stringconverter并通过返回一个string数组来实现下拉:
没有特别之处,只是当我们重写getstandardvaluesexclusive为true是,我们就不能手动输入字符串,只能根据下拉选择值.
樱花校园模拟器4399中文版
樱花校园模拟器4399中文版,十分奇趣好玩的3d校园冒险模拟
我的暑假秘密回忆繁星新春版
我的暑假秘密回忆繁星新春版是一款少女模拟养成类休闲游戏。这款
樱花校园模拟器国际版
樱花校园模拟器国际版,一个超级自由超级好玩的自由恋爱模拟的游
饥荒海难九游版
饥荒海难九游版,带你去真实的海岛上感受一下什么才是真正的孤岛
盖瑞模组体验版最新版
盖瑞模组体验版是玩法非常丰富多元化的沙盒建造游戏,玩家可以在