question

fzkim-3516 avatar image
0 Votes"
fzkim-3516 asked JackJJun-MSFT commented

How to implement Search function in winforms?

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;
        }




dotnet-csharpwindows-forms
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

But SearchNode method is not working with this error NullReferenceException.

Which line of code is throwing the exception?

0 Votes 0 ·
fzkim-3516 avatar image fzkim-3516 karenpayneoregon ·

@karenpayneoregon It's line 15. Thank you.

0 Votes 0 ·

@fzkim-3516, is any update? Do you have time to check if my answer works for you.

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@fzkim-3516, 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.


2.gif (301.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.