This is a migrated thread and some comments may be shown as answers.

How to find a treenode based on the value entered in a text box

3 Answers 1400 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Casey
Top achievements
Rank 1
Casey asked on 24 Aug 2009, 06:22 PM
Ok, I have a text box that the users can input an organization. Once they click search there is a method that loops through the tree to find any node that contains the value entered in the text box. There could be occurences where multiple nodes in the tree contain what the user enters, however, the search stops and the first one found from the top. I would like to add the functionality to enable the user to click search again and the next tree node that contains the search value would be selected, rather than the first node encountered that contains the value.

Here is the code I have to I currently use to accomplish the first part. The Search button's on_click event is SearchButton_Click. I'm using Q1 2009.

private void FindRecursive(RadTreeNode treeNode, string searchString, Boolean foundFirst)  
    {  
        Boolean found = foundFirst;  
        foreach (RadTreeNode tn in treeNode.Nodes)  
        {  
              
            if (tn.Text.ToUpper().Contains(searchString) && (!found))  
            {  
                found = true;  
                tn.Selected = true;  
                tn.Focus();  
            }  
            FindRecursive(tn, searchString.ToUpper(), found);  
        }  
    }  
 
 protected void SearchButton_Click(object sender, EventArgs e)  
    {  
        if (Org_Emp_Srch.Text.Trim() != "")  
            {  
                string SearchStr = Org_Emp_Srch.Text.ToUpper();  
                FindRecursive(radtreeview1.Nodes[0], SearchStr, false);  
            }  
        radtreeview1.ExpandAllNodes();  
    } 

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Aug 2009, 07:09 AM
Hi Casey,

Try the following code snippet for searching nodes in RadTreeView on each button click.

C#:
 
protected void Button1_Click(object sender, EventArgs e) 
    string SearchStr = "Child RadTreeNode 1"// Search string 
    RadTreeView1.ExpandAllNodes(); 
    FindNode(RadTreeView1, SearchStr);   
 
private void FindNode(RadTreeView tree, string searchString) 
    Boolean selectedfound = false
    for (int i = 0; i < tree.GetAllNodes().Count; i++) 
    { 
        if (RadTreeView1.SelectedNode != null
        { 
            if (tree.GetAllNodes()[i].Text.Contains(searchString) && selectedfound) 
            { 
                tree.GetAllNodes()[i].Selected = true
                tree.GetAllNodes()[i].Focus(); 
                return
            } 
            if( tree.GetAllNodes()[i].Selected == true ) 
            { 
                selectedfound = true
            } 
        } 
        else 
        { 
            RadTreeView1.FindNodeByText(searchString).Selected = true
            return
        } 
    } 
}   

-Shinu.
0
Casey
Top achievements
Rank 1
answered on 25 Aug 2009, 12:51 PM
Hi Shinu,

Thank you for the suggestion; however, I receive an error at the following line of the code:

radtreeview1.FindNodeByText(searchString,

true).Selected = true;

NullReferenceException: Object reference not set to an instance of an object.

When I comment out the "else" section, the code runs and the first treenode containing the searchString is selected. Subsequent searches find the same treenode, not the next node that contains the searchString.

Does the FindNodeByText only try to match the string with the beginning of the node text, or if a node contains the string should it be found? Does it act like: TreeNode.Text = string, TreeNode.Text.StartsWith(string), TreeNode.Text.Contains(string)?? If it behaves like the first  or second situation then that might be why I am experiencing this issue, as the text I am searching for is not the leading part of the TreeNode.Text, nor does it match the TreeNode.Text perfectly. I also added the TRUE option to the FindNodeByText function to ignore case when searching.

 

0
Casey
Top achievements
Rank 1
answered on 25 Aug 2009, 04:00 PM
The below code accomplishes what I was looking for:

protected void Page_Load(object sender, EventArgs e)  
{  
    if (!Page.IsPostBack)  
        {  
            Session.Add("NodeInt", -1);  
        }  


protected void Button_Click(object sender, EventArgs e)  
{  
     int nodeCount = radtreeview1.GetAllNodes().Count;  
     string SearchStr = "Search String"// Search string  
     FindNode(radtreeview1, SearchStr, nodeCount);  


    private void FindNode(RadTreeView tree, string searchString, int nodeCount)  
    {  
        Boolean selectedfound = false;  
        for (int i = (int)Session["NodeInt"] + 1; i < nodeCount; i++)  
        {  
            if (tree.GetAllNodes()[i].Text.ToUpper().Contains(searchString.ToUpper()))  
            {  
                tree.GetAllNodes()[i].Selected = true;  
                tree.GetAllNodes()[i].Focus();  
                Session.Add("NodeInt", i);  
                selectedfound = true;  
                return;  
            }  
        }  
        if (selectedfound == false)  
        {  
            Session.Add("NodeInt", -1);  
        }  
    } 

This resets the session variable if the user changes what they are searching in the middle of the tree.
protected void Org_Emp_Srch_TextChanged(object sender, EventArgs e)  
    {  
        Session.Add("NodeInt", -1);  
    } 
Tags
TreeView
Asked by
Casey
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Casey
Top achievements
Rank 1
Share this question
or