Highlight all searched words

后端 未结 6 754
耶瑟儿~
耶瑟儿~ 2020-12-16 00:35

In my RichtextBox, if I have written as below.

This is my pen,
his pen is beautiful.

Now I search word \"is\"

6条回答
  •  情深已故
    2020-12-16 01:13

    This will show all the searched criteria at the same time.

    Using: 1 Textbox (to enter the text to search for) and 1 Button (to Run the Search).

    Enter your search criteria inside the textbox and press search button.

            // On Search Button Click: RichTextBox ("rtb") will display all the words inside the document
        private void btn_Search_Click(object sender, EventArgs e)
        {
            try
            {
                if (rtb.Text != string.Empty)
                {// if the ritchtextbox is not empty; highlight the search criteria
                    int index = 0;
                    String temp = rtb.Text;
                    rtb.Text = "";
                    rtb.Text = temp;
                    while (index < rtb.Text.LastIndexOf(txt_Search.Text))
                    {
                        rtb.Find(txt_Search.Text, index, rtb.TextLength, RichTextBoxFinds.None);
                        rtb.SelectionBackColor = Color.Yellow;
                        index = rtb.Text.IndexOf(txt_Search.Text, index) + 1;
                        rtb.Select();
                    }
                }
            }
    
            catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); }
        }
    }
    

    }

提交回复
热议问题