Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
}

In my MainForm:

private void button1_Click(object sender, EventArgs e)
{
   UserControl userControl = new UserControl();
   userControl.Refresh();
}
Posted
Updated 25-May-10 18:49pm
v2

1 solution

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


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?

[EDIT]
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 wrote:
public override void Refresh()
{
PopulateTreeView();
}

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

[RESPONSE TO COMMENT]
Samiul Aman wrote:
Is it OK?

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]
 
Share this answer
 
v3
Comments
Samiul Aman 26-May-10 8:00am    
I've added the UserControl on a TabPage of a TabControl. So the code becomes
this.myTabPage.Controls.Add(this.userControl);

Is it OK?

Another thing, I can't understand about the base.Refresh() part.
Where exactly should I use it?

Thanks again.
Samiul Aman 26-May-10 10:24am    
Thanks brother. I've solved it through your answer. What happened was, instead of using public Property for private variables inside the UserControl class, I've been passing variable through it's constructor and as a result, I've been calling 'new UserControl(string s)' every time in the click-handler. Thanks again for the -- 'If there's only one user control then there's definitely no need to call new UserControl() in the click handler' part.

Also, base.Refresh() method is also helpful.

Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900