Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi all, I would like to select one of the child node of my treeview get selected programatically. My treeview is as follow at run time
      Root
       |-A.txt(I would like to select this node after doing some iteration in my application)
         |-Child(Even if i select this node and do some operations i would like toselect the above one only like that if n number of child nodes exists i would like to select the node that was with .txt extension)




I write my code as follows it works fine but for the last node which has no sub nodes it is not working fine can any modify this and give


C#
if (tvwACH.Nodes.Count != 0)
                {
                   // tvwACH.ExpandAll();
                    TreeNode tn;
                    tn = tvwACH.Nodes[0];
                    tvwACH.ExpandAll();
                    if  (tn.Nodes.Count != 0)
                    {
                        tn = tn.Nodes[0];
                    }
                    if (tn.Tag.ToString() == "3")
                    {
                        if (tvwACH.SelectedNode.Parent != null)
                        {
                            tn.Parent.Expand();
                            tvwACH.SelectedNode = tn;
                        }
                    }
                   }


My treeview is as follows

Root
            |->Some.txt
                |->CHild
                  |->Child1
                    |->Child2(After this i am unable to select the one i required



Any idea please
Posted
Updated 3-Oct-10 22:47pm
v2
Comments
demouser743 4-Oct-10 11:59am    
can any one help me please i got struck at this point in my application

You should do this using recursion.. it become much simpler
C#
bool Select_It(string str, TreeView tvwParent, TreeNode tN)
{
    if (tN.Text == str)
    { tvwParent.SelectedNode = tN; return true; }
    foreach (TreeNode child in tN.Nodes)
    {
        if (Select_It(str, tvwParent, tN)) { return true; }
    }
    return false;
}




bool Select_It_By_Tag_Value(string str, TreeView tvwParent, TreeNode tN)
{
    if (tN.Tag.ToString() == str)
    { tvwParent.SelectedNode = tN; return true; }
    foreach (TreeNode child in tN.Nodes)
    {
        if (Select_It(str, tvwParent, tN)) { return true; }
    }
    return false;
}

//then use:
void select_node()
{
    if (Select_It_By_Tag_Value("3", tvwACH, tvwACH.Nodes[0]))
    {
        //value exists
    }
    else
    {
        // value not in tree
    }
}
 
Share this answer
 
v2
Comments
demouser743 5-Oct-10 3:51am    
Hi here "3" refers to the tag node of treeview.
I got the answer


C#
TreeNode TvNode = tvwACH.SelectedNode.Parent;
           while (TvNode != null)
           {
               if (TvNode.Text.EndsWith(".txt", true, System.Globalization.CultureInfo.InvariantCulture))
               {
                   tvwACH.SelectedNode = TvNode;
                   TvNode = null;
               }
               else
                   TvNode = TvNode.Parent;
           }
 
Share this answer
 

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