UserControl/TreeView问题....在运行时未得到结果. [英] UserControl/TreeView Problem....Not getting result during runtime..

查看:58
本文介绍了UserControl/TreeView问题....在运行时未得到结果.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的问题是,我在UserControl中使用TreeView.在调试时,我可以看到结果正在添加到TreeView中,但是当我在MainForm上使用该UserControl时却没有发生.包含TreeView的UserControl在主应用程序运行期间保持空白.我还在我的Main项目中引用了UserControl项目.在这里,我给出了帮助我的代码.

提前谢谢.

代码:
在UserControl类中:

Hi all,

My problem is, I''m using a TreeView in a UserControl. While debugging, I can see the results are adding in the TreeView but it''s not happening when I''m using that UserControl onto my MainForm. The UserControl containing the TreeView remains blank during runtime of the main application. I''ve also referenced the UserControl project with my Main project. Here I''m giving my code for helping me out.

Thanks in advance.

Code:
In the UserControl class:

public override void Refresh()
{
   PopulateTreeView();
}
private void PopulateTreeView()
{
   TreeNodeCollection treeNodeCollection;	
   treeNodeCollection = CreateParentNode("My Information");
   CreateChildNode(treeNodeCollection, "Name");
   CreateChildNode(treeNodeCollection, "Address");
   this.Update();
   myTreeView.ExpandAll();
}
private TreeNodeCollection CreateParentNode(string parentNode)
{
   TreeNode treeNode = new TreeNode(parentNode);
   myTreeView.Nodes.Add(treeNode);
   return treeNode.Nodes;
}
private void CreateChildNode(TreeNodeCollection nodeCollection, string itemName)
{
   TreeNode treeNode = new TreeNode(itemName);
   nodeCollection.Add(treeNode);
}


在我的MainForm中:


In my MainForm:

private void button1_Click(object sender, EventArgs e)
{
   UserControl userControl = new UserControl();
   userControl.Refresh();
}

推荐答案

Samiul Aman写道:
Samiul Aman wrote:

私有无效button1_Click(对象发送者,EventArgs e)
{
UserControl userControl = new UserControl();
userControl.Refresh();
}

private void button1_Click(object sender, EventArgs e)
{
UserControl userControl = new UserControl();
userControl.Refresh();
}



这个新的UserControl是否被添加到表单的任何地方?如果不执行this.Controls.Add(userControl);并设置控件的其他属性(位置,可见等),则该窗体将永远不会绘制它.您确定要为每次按钮单击创建一个新的用户控件,而不是仅在创建表单时创建/添加一个按钮吗?


您可能打算实例化一个 new MyDerivedUserControl()而不是仅实例化一个new UserControl().那肯定会引起您的悲伤:)
[/EDIT]



Is this new UserControl being added to the form anywhere? If you don''t do a this.Controls.Add(userControl); and set the control''s other properties (location, visible, etc) then it''ll never be drawn by the form. Are you sure that you want to create a new User Control for every button click instead of just creating/adding one when the form is created?


You probably meant to instantiate a new MyDerivedUserControl() instead of just a new UserControl() there as well. That will definitely cause you grief :)
[/EDIT]

Samiul Aman写道:
Samiul Aman wrote:

公共替代无效Refresh()
{
PopulateTreeView();
}

public override void Refresh()
{
PopulateTreeView();
}


完成自己的自定义设置后,调用base.Refresh()也是一个好主意.

[回应评论]


It also might be a good idea to call base.Refresh() after you''ve done your own custom stuff.

[RESPONSE TO COMMENT]

萨米尔·阿曼写道:

可以吗?


我不知道,这行得通吗?如果您有this.userControl
变量,然后我要问:每次单击按钮都会将新的用户控件添加到新的选项卡页面,还是每次单击按钮都只应刷新一个用户控件?如果只有一个用户控件,则绝对不需要在点击处理程序中调用new UserControl().

我对调用base.Refresh()的评论是,您在自己的类中覆盖了该方法的默认行为.调用PopulateTreeView();之后,应考虑调用该方法的基本版本,以便用户控件可以执行其通常的操作.
[/回应评论]


I don''t know, does it work? If you have a this.userControl
variable then I have to ask: does a new user control get added to a new tab page every time the button is clicked, or is there only one user control that should be refreshed every time the button is clicked? If there''s only one user control then there''s definitely no need to call new UserControl() in the click handler.

My comment about calling base.Refresh() is that you overrode the default behavior for that method in you own class. After the call to PopulateTreeView(); you should consider calling the base version of the method so that the user control can do what it normally does.
[/RESPONSE TO COMMENT]


这篇关于UserControl/TreeView问题....在运行时未得到结果.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆