How to implement Search function in winforms?

asked 2022-06-20T06:14:55.993+00:00
fzkim 61 Reputation points

I'd like to implement a search function that appears when we press ctrl+F.
So when I press Ctrl+F, I can make another Form and send value.
But SearchNode method is not working with this error NullReferenceException.
How can I make search function that works in treeview?

  • Form1.cs public TreeView TreeView1 { get; set; } protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    if (keyData == (Keys.Control | Keys.F))
    {
    Search search = new Search();
           search.ChildFormEvent += EventMethod;  
    
           search.ShowDialog();  
           return true;  
       }  
       return base.ProcessCmdKey(ref msg, keyData);  
    
    } public void EventMethod(string str)
    {
    textBox1.Text = str.ToString();
    }
  • Form2.cs public delegate void ChildFormSnedDataHandler(string message);
    public event ChildFormSnedDataHandler ChildFormEvent;
           public void btnSearch_Click(object sender, EventArgs e)  
           {  
               string str = string.Empty;  
               str = txtSearch.Text;  
    
               this.ChildFormEvent(str);  
    
               this.Close();  
               Form1 form1 = new Form1();  
               try  
               {  
                   TreeNode SelectedNode = SearchNode(str, form1.TreeView1.Nodes[0]);  
                   if (SelectedNode != null)  
                   {  
                       form1.TreeView1.SelectedNode = SelectedNode;  
                       form1.TreeView1.SelectedNode.Expand();  
                       form1.TreeView1.Select();  
                   }  
               }  
               catch (Exception ex)  
               {  
                   MessageBox.Show(ex.Message);  
               }  
           }  
    
           private TreeNode SearchNode(string SearchText, TreeNode StartNode)  
           {  
               TreeNode node = null;  
               while (StartNode != null)  
               {  
                   if (StartNode.Text.ToLower().Contains(SearchText.ToLower()))  
                   {  
                       node = StartNode;  
                       break;  
                   }  
                   if (StartNode.Nodes.Count != 0)  
                   {  
                       node = SearchNode(SearchText, StartNode.Nodes[0]);  
                       if (node != null)  
                           break;  
                   }  
                   StartNode = StartNode.NextNode;  
               }  
               return node;  
           }  
    
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,317 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,939 questions
{count} votes

1 answer

Sort by: Most helpful
  1. answered 2022-06-21T07:10:44.93+00:00
    Jack J Jun 19,046 Reputation points Microsoft Employee

    @fzkim , Welcome to Microsoft Q&A, based on my test, I reproduced your problem.

    Please make the following changes.

    First, Please set your treeview1 for the property TreeView1:

    public TreeView TreeView1  
            {  
                get  
                {  
                    return treeView1;  
                }  
                set  
                {  
                    treeView1 = value;  
                }  
            }  
    

    Second, we need to use Form.Show() Method instead of Form.ShowDialog(), because it will not update form1's control when we use ShowDialog() method to show form2.

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  
            {  
                if (keyData == (Keys.Control | Keys.F))  
                {  
                    str = textBox1.Text;  
                    Form2 search = new Form2();  
                    search.Show();  
                    return true;  
                }  
                return base.ProcessCmdKey(ref msg, keyData);  
            }  
    

    Third, you could try to use Application.Openforms to transfer value between forms.

     Form1 form1 = (Form1)Application.OpenForms["Form1"];  
                txtSearch.Text = form1.str;  
    

    Finally, Please refer to the following code example:

    Form1:

     public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
            public TreeView TreeView1  
            {  
                get  
                {  
                    return treeView1;  
                }  
                set  
                {  
                    treeView1 = value;  
                }  
            }  
      
            public string str { get; set; }  
            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  
            {  
                if (keyData == (Keys.Control | Keys.F))  
                {  
                    str = textBox1.Text;  
                    Form2 search = new Form2();  
                    search.Show();  
                    return true;  
                }  
                return base.ProcessCmdKey(ref msg, keyData);  
            }  
      
            private void Form1_Load(object sender, EventArgs e)  
            {  
                treeView1.ExpandAll();  
                  
            }  
        }  
    

    Form2:

      public partial class Form2 : Form  
        {  
            public Form2()  
            {  
                InitializeComponent();  
            }  
            //public delegate void ChildFormSnedDataHandler(string message);  
            //public event ChildFormSnedDataHandler ChildFormEvent;  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                Form1 form1 = (Form1)Application.OpenForms["Form1"];  
                txtSearch.Text = form1.str;  
                try  
                {  
                    TreeNode SelectedNode = SearchNode(txtSearch.Text, form1.TreeView1.Nodes[0]);  
                    if (SelectedNode != null)  
                    {  
                        form1.TreeView1.SelectedNode = SelectedNode;  
                        form1.TreeView1.SelectedNode.Expand();  
                        form1.TreeView1.Focus();  
                    }  
                }  
                catch (Exception ex)  
                {  
                    MessageBox.Show(ex.Message);  
                }  
                finally  
                {  
                    this.Close();  
                }  
            }  
      
            private TreeNode SearchNode(string SearchText, TreeNode StartNode)  
            {  
                TreeNode node = null;  
                while (StartNode != null)  
                {  
                    if (StartNode.Text.ToLower().Contains(SearchText.ToLower()))  
                    {  
                        node = StartNode;  
                        break;  
                    }  
                    if (StartNode.Nodes.Count != 0)  
                    {  
                        node = SearchNode(SearchText, StartNode.Nodes[0]);  
                        if (node != null)  
                            break;  
                    }  
                    StartNode = StartNode.NextNode;  
                }  
                return node;  
            }  
        }  
    

    Tested result:

    213291-2.gif

    Best regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments