Node searching in TreeView
-
Hello, I got a problem. I've made a FolderTree by using TreeView control. Now, I want to search one node that represents the user typed directory path in textBox. I have no idea to do this. How can I search that node. e.g. user typed : C:\folder1\temp then the tree control expanded at temp. Please Help, and could anyone show me some example?? Thank you very much!!
-
Hello, I got a problem. I've made a FolderTree by using TreeView control. Now, I want to search one node that represents the user typed directory path in textBox. I have no idea to do this. How can I search that node. e.g. user typed : C:\folder1\temp then the tree control expanded at temp. Please Help, and could anyone show me some example?? Thank you very much!!
Here are a little approach ...
public TreeNode Search(TreeNodeCollection col) { // Create an enumerator for the collection. IEnumerator myEnumerator = col.GetEnumerator(); TreeNode node = null; while(myEnumerator.MoveNext()) { System.Console.Out.WriteLine(((TreeNode)myEnumerator.Current).Text + ": "+ ((TreeNode)myEnumerator.Current).FullPath); if ( ((TreeNode)myEnumerator.Current).FullPath == textBox1.Text) // Make the right comparision return ((TreeNode)myEnumerator.Current); else { node = Search( ((TreeNode)myEnumerator.Current).Nodes ); } } return node; } private void button1_Click(object sender, System.EventArgs e) { TreeNode node = Search(treeView1.Nodes); if (node!=null) { treeView1.SelectedNode = node; treeView1.Select(); } }
I hope that it helps. Greetings, Jose. -
Here are a little approach ...
public TreeNode Search(TreeNodeCollection col) { // Create an enumerator for the collection. IEnumerator myEnumerator = col.GetEnumerator(); TreeNode node = null; while(myEnumerator.MoveNext()) { System.Console.Out.WriteLine(((TreeNode)myEnumerator.Current).Text + ": "+ ((TreeNode)myEnumerator.Current).FullPath); if ( ((TreeNode)myEnumerator.Current).FullPath == textBox1.Text) // Make the right comparision return ((TreeNode)myEnumerator.Current); else { node = Search( ((TreeNode)myEnumerator.Current).Nodes ); } } return node; } private void button1_Click(object sender, System.EventArgs e) { TreeNode node = Search(treeView1.Nodes); if (node!=null) { treeView1.SelectedNode = node; treeView1.Select(); } }
I hope that it helps. Greetings, Jose.