4 Replies - 1963 Views - Last Post: 08 October 2012 - 06:07 AM

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Find text in a RichTextBox

Posted 31 August 2008 - 03:02 AM

Description: Pass the text you're looking for, whether to match case or not and the RichTextBox you're looking in.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
/// 
/// method for searching for specified text in a RichTextBox
/// 
/// text we're looking for
/// match case or not?
/// RichTextBox we're searching for
public static void Find(string text, bool matchCase, System.Windows.Forms.RichTextBox rtb)
{
    try
    {
        //variable to hold the start position (start of the found text)
        int startPos;

        //what kind of search are we doing
        StringComparison type;

        //determine if it's a match case search or not
        type = matchCase == true ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

        //look for the search text
        startPos = rtb.Text.IndexOf(text, type);

        //check the position
        if (!(startPos > 0))
        {
            //text doesn't exist so let the user know
            MessageBox.Show("Search text: '" + text + "' could not be found", "Text Not Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            return;
        }
        else
        {
            //Yureka! Select the found text
            rtb.Select(startPos, text.Length);
            //scroll to the found text
            rtb.ScrollToCaret();
            //add focus so the highlighting shows up
            rtb.Focus();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Search Error");
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Find text in a RichTextBox

#2 wingot  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 13-October 08

Re: Find text in a RichTextBox

Posted 20 October 2008 - 08:16 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 vikramjit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 24-December 08

Re: Find text in a RichTextBox

Posted 25 December 2008 - 04:01 AM

it do not search for the word if the word is at the first position
Was This Post Helpful? 0
  • +
  • -

#4 dj_limi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 01-May 09

Re: Find text in a RichTextBox

Posted 01 May 2009 - 03:43 PM

wingot how do you make nextStartPos can u help me with this code thx in advance
Was This Post Helpful? 0
  • +
  • -

#5 zeeshanef  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 77
  • Joined: 14-April 12

Re: Find text in a RichTextBox

Posted 08 October 2012 - 06:07 AM

@vikramjit: add [>=] in line 24 instead [>]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1