Click here to Skip to main content
15,921,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using windows form application in c#.
I have a textbox and and treeview with data..
Now i want to select the nodes in treeview according to the textbox.text so can anyone help me???
Posted
Updated 9-Aug-13 23:09pm
v2
Comments
OriginalGriff 10-Aug-13 5:31am    
What have you tried?
Where are you stuck?
What help, basically, do you you need?
NoorKaximi 10-Aug-13 5:41am    
i haven't tried anything yet..

Here i just put some basic code,Try out this..I assume that it work for your requirement.

VB
Dim FirstTreeViewNode As Boolean = True

        For Each TreeViewNode As TreeNode In TreeView1.Nodes(0).Nodes
            If FirstTreeViewNode Then
                TextBox2.Text = TreeViewNode.Text
                FirstTreeViewNode = False
            Else
                TextBox2.AppendText(String.Concat(Constants.vbCrLf, Constants.vbCrLf, TreeViewNode.Text))
            End If
        Next


Thanks

Renish

Happy Coding..
 
Share this answer
 
v3
Comments
NoorKaximi 10-Aug-13 6:05am    
I am using c# not visual basic
submit codes for c#
renish patel 10-Aug-13 6:25am    
Its to much easy to understand...not matter its vb or c#

bool FirstTreeViewNode = true;

foreach (TreeNode TreeViewNode in TreeView1.Nodes(0).Nodes) {
if (FirstTreeViewNode) {
TextBox2.Text = TreeViewNode.Text;
FirstTreeViewNode = false;
} else {
TextBox2.AppendText(string.Concat(Constants.vbCrLf, Constants.vbCrLf, TreeViewNode.Text));
}
}
by these 2 method you can get your target node. first method selected a node by NODE TEXT and second method select by NODE NAME.
after finding your target node do like this:

treeView1.SelectedNode = GetNodeByName(Textbox1.Text);



private TreeNode GetNodeByName(TreeNodeCollection nodes, string searchtext)
      {
          TreeNode n_found_node = null;
          bool b_node_found = false;

          foreach (TreeNode node in nodes)
          {

              if (node.Name == searchtext)
              {
                  b_node_found = true;
                  n_found_node = node;

                  return n_found_node;
              }

              if (!b_node_found)
              {
                  n_found_node = GetNodeByName(node.Nodes, searchtext);

                  if (n_found_node != null)
                  {
                      return n_found_node;
                  }
              }
          }
          return null;
      }

      private TreeNode GetNodeByText(TreeNodeCollection nodes, string searchtext)
      {
          TreeNode n_found_node = null;
          bool b_node_found = false;

          foreach (TreeNode node in nodes)
          {

              if (node.Text == searchtext)
              {
                  b_node_found = true;
                  n_found_node = node;

                  return n_found_node;
              }

              if (!b_node_found)
              {
                  n_found_node = GetNodeByText(node.Nodes, searchtext);

                  if (n_found_node != null)
                  {
                      return n_found_node;
                  }
              }
          }
          return null;
      }
 
Share this answer
 
Comments
NoorKaximi 10-Aug-13 6:15am    
i Tried these codes but it doesn't work
private void textBox1_TextChanged(object sender, EventArgs e)
{

treeView1.SelectedNode = GetNodeByText(treeView1.Nodes,txtsearch.Text);

}
private TreeNode GetNodeByText(TreeNodeCollection nodes, string searchtext)
{
TreeNode n_found_node = null;
bool b_node_found = false;

foreach (TreeNode node in nodes)
{

if (node.Text == searchtext)
{
b_node_found = true;
n_found_node = node;

return n_found_node;
}

if (!b_node_found)
{
n_found_node = GetNodeByText(node.Nodes, searchtext);

if (n_found_node != null)
{
return n_found_node;
}
}
}
return null;
}
Morteza Karimian Kelishadrokhi 11-Aug-13 7:48am    
i use of first solution in my project and does not exist any problem. but i posted another method via LINQ, I tesed this and work accurate.try it...

TreeNode TargetNode = treeView1.Nodes.Cast<TreeNode>().ToList().Find(n => n.Text.Equals(TargetText));

treeView1.SelectedNode =TargetNode ;



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