3

i have load a tree view. i want to Traverse treeview node and expand & select a node. Expand is working fine. but select a node is not working.

private void Traverse(TreeNodeCollection nodes, string findtext) 
        {
          foreach (TreeNode node in nodes) 
            {
                if (node.Text.ToString().Trim() == findtext)
                {
                    node.Expand();
                    node.TreeView.SelectedNode = node.NextNode;                    

                    //tvwStructureTree.SelectedNode = this.tvwStructureTree.Nodes[node.Index];
//Select a node in Treeview tvwStructureTree But not working
                    tvwStructureTree.SelectedNode = node; 
                    node.TreeView.Focus(); 
                }
                Traverse(node.Nodes, findtext); 
            } 

        }

Its in windows application.

1
  • What node do you want to select? Not node.NextNode I imagine. What is tvwStructureTree? Try node.TreeView.SelectedNode = node.Nodes[0]; Commented Oct 12, 2011 at 8:48

3 Answers 3

5

Not quite sure what's your issue is. TreeView.SelectedNode Property is the correct property.

When you set this property, the specified node is scrolled into view and any parent nodes are expanded so that the specified node is visible.

When the parent node or any ancestor node of the selected node is collapsed either programmatically or through user action, the collapsed node becomes the selected node.

Make sure that the node.TreeView is the correct TreeView instance.

node.TreeView.SelectedNode = node.NextNode;  

TreeView.HideSelection Property is another property that might useful for you.

When this property is set to false, selected nodes in the TreeView control remain highlighted in a different color than the current selection color when the TreeView control loses focus. You can use this property to keep items that are selected by the user visible when the user clicks a different control on the form or moves to a different window.

Sign up to request clarification or add additional context in comments.

Comments

1

I had a similar issue. My form's ctor is given the test of a node to select.Finding the correct node was not a problem, but the tree didn't show the node as selected, since the tree control didn't have focus. merely had to use Form.ActiveControl = myTreecontrol; before setting myTreecontrol.SelectedNode

Comments

0

I tested exactly your own code and worked fine, both find and selection the node! without any particular property setting for my treeview! by the way I am using .Net 3.5 and VS 2008

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.