Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 149,621 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,908 people online right now. Registration is fast and FREE... Join Now!





Find text in a RichTextBox

This is a snippet I use to find specified text in a RichTextBox. I use it as a part of a Find And Replace system

Submitted By: PsychoCoder
Actions:
Rating:
Views: 1,162

Language: C#

Last Modified: August 31, 2008
Instructions: Pass the text you're looking for, whether to match case or not and the RichTextBox you're looking in.

Snippet


  1. /// <summary>
  2. /// method for searching for specified text in a RichTextBox
  3. /// </summary>
  4. /// <param name="text">text we're looking for</param>
  5. /// <param name="matchCase">match case or not?</param>
  6. /// <param name="rtb">RichTextBox we're searching for</param>
  7. public static void Find(string text, bool matchCase, System.Windows.Forms.RichTextBox rtb)
  8. {
  9.     try
  10.     {
  11.         //variable to hold the start position (start of the found text)
  12.         int startPos;
  13.  
  14.         //what kind of search are we doing
  15.         StringComparison type;
  16.  
  17.         //determine if it's a match case search or not
  18.         type = matchCase == true ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
  19.  
  20.         //look for the search text
  21.         startPos = rtb.Text.IndexOf(text, type);
  22.  
  23.         //check the position
  24.         if (!(startPos > 0))
  25.         {
  26.             //text doesn't exist so let the user know
  27.             MessageBox.Show("Search text: '" + text + "' could not be found", "Text Not Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  28.             return;
  29.         }
  30.         else
  31.         {
  32.             //Yureka! Select the found text
  33.             rtb.Select(startPos, text.Length);
  34.             //scroll to the found text
  35.             rtb.ScrollToCaret();
  36.             //add focus so the highlighting shows up
  37.             rtb.Focus();
  38.         }
  39.     }
  40.     catch (Exception ex)
  41.     {
  42.         MessageBox.Show(ex.Message, "Search Error");
  43.     }
  44. }

Copy & Paste


Comments


wingot 2008-10-20 23:16:08

Thanks. Only had to change the method from static to normal and add a "nextStartPos" variable and this works perfectly in a find dialogue analogous to notepads (i.e., Each time you click "next" it finds the next occurence). I also modified the RichTextBox to a normal TextBox but both should be about the same.

vikramjit 2008-12-25 06:01:31

it do not search for the word if the word is at the first position


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month