Search a words and highlight them (C#)

In this post I will show you how search a word and highlight it
  1. Open the last C# project
  2. Add a TextBox and a Button
  3. I change the button's name as Search
  4. Double click on the search button and put the following codes.
  5.             int index = 0;
                string temp = GetRichBox().Text;
                GetRichBox().Text = "";
                GetRichBox().Text = temp;
                while (index < richTextBox1.Text.LastIndexOf(textBox1.Text))
                {
                    
                    richTextBox1.Find(textBox1.Text, index, richTextBox1.TextLength, RichTextBoxFinds.None);
                    richTextBox1.SelectionBackColor = Color.GreenYellow;
                    index = richTextBox1.Text.IndexOf(textBox1.Text, index) + 1;
                }
            
  6. Now run your programm
  7. You can see the letters was highlighted, I typed
  8. You can change the Events of the text box
  9. Goto propeties -> Events
  10. Search for a KeyPress event.
  11. Double click on it and goto code part of it.
  12. Copy the all code of search button and paste it in KeyPress event.
  13. Now you don'd need the search button. If you type a key, it will search for it.
Next post I will show you how remove the tabs.

No comments:

Post a Comment