栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

c#模拟看板控件

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c#模拟看板控件

在实现了一个自定义控件后,如果希望在VS的界面编辑器中能对其进行操作,则必须对其添加Designer以及相应的属性:

[Designer(typeof(MyDesigner))]

[DesignTimeVisible(true)]public partial classUserControl1 : UserControl

{

……

[Browsable(false)] //在控件的属性设置列表中不可见

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]publicTabControl MyTabControl //用于Designer类中设置可编辑的子控件

{get{return this.tabControl1;

}

}

……

}internal classMyDesigner : ParentControlDesigner

{privateUserControl1 MyControl;public override voidInitialize(IComponent component)

{

……

MyControl = (UserControl1)component;

……

}

}

以上代码中,设计支持类MyDesigner 继承于ParentControlDesigner,表示该控件可以作为容器使用,这样就可以使得自定义控件成为容器。

如果不想让控件作为容器使用,而仅仅是需要在使用该控件的时候需要界面编辑器也能够操作UserControl中的控件,则继承ControlDesigner类即可。

为了让UserControl中的控件能够在设计器中也能够被操作,需要使用EnableDesignMode函数将控件设置为设计模式可编辑模式。

特别地,对于TabControl而言,还需要使用EnableDesignMode设置它的每一个TabPage,否则Tab页是无法添加控件的。

此外,TabControl只有在设计时新添加的TabPage才能够在上面添加控件,在UserControl中已经添加的虽然在设计器上可以添加控件,但是在设计器生成的代码中,并不会生成将控件添加到TabControl上的代码段,这事有点奇特,不知道是不是微软的BUG?

完整代码如下:

[Designer(typeof(MyDesigner))]

[DesignTimeVisible(true)]public partial classUserControl1 : UserControl

{publicUserControl1()

{

InitializeComponent();

}[Browsable(false)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]publicTabControl MyTabControl

{get{return this.tabControl1;

}

}

}internal classMyDesigner : ParentControlDesigner

{privateUserControl1 MyControl;public override voidInitialize(IComponent component)

{base.Initialize(component);

MyControl=(UserControl1)component;bool succ = this.EnableDesignMode(MyControl.MyTabControl, "MyTabControl");foreach (TabPage item inMyControl.MyTabControl.TabPages)

{

succ=EnableDesignMode(item, item.Name);

}

}

}

Reference:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/911347.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号