关于winform的UserControl的问题

mousetrue 2005-01-18 03:05:46
我要建立一个UserControl这个UserControl上面我放了一个ToolBar,并且把他做为一个Public属性公开。
public class MyUserControl: System.Windows.Forms.UserControl
{
private System.Windows.Forms.ToolBar toolBar1;
public ToolBar TBar
{
get{return toolBar1;}
set{toolBar1 = value;}
}
}
然后我把这个UserControl放到一个Form上面。在设计期对这个Form上的UserControl的TBar属性进行设置,我增加了几个ToolButton
然后确定,运行以后。这几个ToolButton不见了。
看他的代码,他的ToolButton增加到了Form上。。但是ToolButton没有增加到ToolBar的集合中。ide没有自动生成这部分的代码
我需要在设计期对这个ToolBar进行设置,并且自动的生成相应的代码,这个问题如何解决?
...全文
545 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
北京的雾霾天 2005-01-20
  • 打赏
  • 举报
回复
有时并不能完全依赖IDE的设计时功能,这个功能是有限的.
如果不是一定要这么做,就换一个思路吧.
hawk234 2005-01-20
  • 打赏
  • 举报
回复
帮顶
mousetrue 2005-01-20
  • 打赏
  • 举报
回复
晕。。。没人理我。。。。我顶
marvelstack 2005-01-20
  • 打赏
  • 举报
回复
try
http://www.csdn.net/develop/Read_Article.asp?id=24936
http://www.csdn.net/develop/Read_Article.asp?id=24937
http://www.csdn.net/develop/Read_Article.asp?id=24938
http://www.csdn.net/develop/Read_Article.asp?id=24939
mousetrue 2005-01-19
  • 打赏
  • 举报
回复
哪位老大知道如何解决??请支招。。
nga96 2005-01-19
  • 打赏
  • 举报
回复
对,最好用属性来实现吧
mooddecode 2005-01-19
  • 打赏
  • 举报
回复
up
mousetrue 2005-01-19
  • 打赏
  • 举报
回复
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ToolBar.ToolBarButtonCollection Buttons
{
get{return toolBar1.Buttons;}
}
我现在测试出。在UserControl里面再增加一个这样的属性。就能达到我的要求了。
加到窗体上以后的生成的代码是这样的
this.myUserControl = new myUserControl();
this.toolBarButton19 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton20 = new System.Windows.Forms.ToolBarButton();
this.myUserControl.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {this.toolBarButton19, this.toolBarButton20});
已经达到了我要的结果。
但是。最后一条语句不理想。我觉得应该是
this.myUserControl.TBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {this.toolBarButton19, this.toolBarButton20});
有什么办法搞定?
mousetrue 2005-01-19
  • 打赏
  • 举报
回复
晕。。。我的代码都贴出来了啊。。就是那些。。那是用户控件的代码,其他的就是把用户控件放到窗体上。然后通过属性编辑器对这个用户控件上的ToolBar进行增加ToolBarButton。
你试一下就知道结果了。
Alden 2005-01-19
  • 打赏
  • 举报
回复
UP
长江 2005-01-19
  • 打赏
  • 举报
回复
你的文字很难理解,你还是把调用代码也贴出来吧
看你也不像新手,应该不会出现大意性的问题
从字眼分析,你也搞这方面也有半年以上了
从你回答的角度分析,你还缺少某方面的敏感
更缺少与大家讨论的积极性
小兄弟,加油啊
mousetrue 2005-01-19
  • 打赏
  • 举报
回复
lonestar5555和jimh你们说的都不对。
jim3说的应该是对的。。但是我不知道具体怎么使用。
jimh 2005-01-18
  • 打赏
  • 举报
回复
public ToolBar TBar
{
get{return toolBar1;}
//set{toolBar1 = value;} 不需要set属性。
}
长江 2005-01-18
  • 打赏
  • 举报
回复
public class MyUserControl: System.Windows.Forms.UserControl
{
private System.Windows.Forms.ToolBar toolBar1;
public ToolBar TBar
{
get{return new toolBar1;}
set{toolBar1 = value;}
}
}
mousetrue 2005-01-18
  • 打赏
  • 举报
回复
Jim3

具体应该怎么用?

msdn上面写的不是很明确。
Jim3 2005-01-18
  • 打赏
  • 举报
回复
看看DesignerSerializationVisibilityAttribute的帮助
mousetrue 2005-01-18
  • 打赏
  • 举报
回复
在什么地方指定父控件?
我是直接在属性编辑器上对ToolBar进行增加ToolButton的。
zsms2 2005-01-18
  • 打赏
  • 举报
回复
没有指定父控件
貓哥是個傳說 2005-01-18
  • 打赏
  • 举报
回复
up

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧