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

4 posts, 0 answers
  1. Casey
    Casey avatar
    299 posts
    Member since:
    Jul 2009

    Posted 24 Aug 2009 Link to this post

    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();  
        } 
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 25 Aug 2009 Link to this post

    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.
  3. Casey
    Casey avatar
    299 posts
    Member since:
    Jul 2009

    Posted 25 Aug 2009 Link to this post

    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.

     

  4. Casey
    Casey avatar
    299 posts
    Member since:
    Jul 2009

    Posted 25 Aug 2009 Link to this post

    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);  
        } 
Back to Top