Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to add child node to parent node of treeview from another form.
I tried this code, this is executing but not adding a child node.
tree view is in form 1. I want to add child node from form 2, textbox value in button ckick event. I did like this
C#
foreach (Form f in Application.OpenForms)
{
    if (f.Name == "form1")
f.controls.contains(treeview1.Nodes["Market"].Nodes.Add(Textbox1.text));
}



can u let me know the problem
Posted
Updated 5-Jan-14 13:39pm
v2
Comments
CHill60 5-Jan-14 18:18pm    
You are creating a new instance of form1 instead of finding the current instance that you already have
hussain548 5-Jan-14 18:23pm    
Ok sir I got your point but how to get current instance?
I know this is silly question but I am new to0 c# so don't min fr this typ0e of question
CHill60 5-Jan-14 18:51pm    
No problems - I couldn't actually remember!! I've posted a solution now
BillWoodruff 5-Jan-14 23:03pm    
It's obvious your code sample will not compile (for several reasons), and I think it's important that when you try to learn a language you do it in the development environment, and actually try executing the code.

That way, as you fix syntax errors, and, perhaps, look up operators, etc., you "learn as you go."

Key information here is:

1. where is the "other" Form created ... in Form1 ?

Try something like below. This code will work properly if there is no more than one treeView located on each open form.

My logic is not difficult to guess. I'm iterating all controls belonging to child form and if I find TreeView control I'm updating "Market" node.



C#
foreach (Form f in Application.OpenForms)
  {
      if (f.Name == "form1")
      {
          foreach (Control ct in f.Controls)
          {
              TreeView treeViewToUpdate = ct as TreeView;
              if (treeViewToUpdate !=null)
                  treeViewToUpdate.Nodes["Market"].Nodes.Add(Textbox1.text));
          }
      }

  }
 
Share this answer
 
Comments
hussain548 6-Jan-14 4:57am    
Sir, it was successful, whenever I re-runs the form it was not stored. means it was not in the collectons.
OP is creating a new instance of form1 so the original never gets updated.

Find the current instance that you have loaded by using Application.OpenForms

E.g.
C#
foreach (Form f in Application.OpenForms)
{
    if (f.Name == "form1")
        //Do something
}
 
Share this answer
 
Comments
hussain548 5-Jan-14 19:09pm    
I appreciate or your help.
foreach (Form f in Application.OpenForms)
{
if (f.Name == "form1")
f.controls.contains(treeview1.Nodes["Market"].Nodes.Add(Textbox1.text));
}

Sir,I did like this but error at treeview 1. I did any mistake sir plz can you help
CHill60 6-Jan-14 7:37am    
Ho hum ... downvoted 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