C# RichTextBox Select specified Text

14,591

Solution 1

     int start = 0;
int indexOfSearchText = 0;
private void btnFind_Click(object sender, EventArgs e)
        {
            int startindex = 0;

            if(txtSearch.Text.Length > 0)
                startindex = FindMyText(txtSearch.Text.Trim(), start, rtb.Text.Length);

            // If string was found in the RichTextBox, highlight it
            if (startindex >= 0)
            {
                // Set the highlight color as red
                rtb.SelectionColor = Color.Red;
                // Find the end index. End Index = number of characters in textbox
                int endindex = txtSearch.Text.Length;
                // Highlight the search string
                rtb.Select(startindex, endindex);
                // mark the start position after the position of
                // last search string
                start = startindex + endindex;
            }
        }




public int FindMyText(string txtToSearch, int searchStart, int searchEnd)
        {
            // Unselect the previously searched string
            if (searchStart > 0 && searchEnd > 0 && indexOfSearchText >= 0)
            {
                rtb.Undo();
            }

            // Set the return value to -1 by default.
            int retVal = -1;

            // A valid starting index should be specified.
            // if indexOfSearchText = -1, the end of search
            if (searchStart >= 0 && indexOfSearchText >=0)
            {
                // A valid ending index
                if (searchEnd > searchStart || searchEnd == -1)
                {
                    // Find the position of search string in RichTextBox
                    indexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
                    // Determine whether the text was found in richTextBox1.
                    if (indexOfSearchText != -1)
                    {
                        // Return the index to the specified search text.
                        retVal = indexOfSearchText;
                    }
                }
            }
            return retVal;
        }



private void textBox1_TextChanged(object sender, EventArgs e)
        {
            start = 0;
            indexOfSearchText = 0;
        }

CheckOut this article if you dont understand this code... http://www.dotnetcurry.com/ShowArticle.aspx?ID=146

Solution 2

You can only have one selection in a text box. What you want is to highlight the found text.
You could achieve it like this:

  1. Find the positions of the text you want to highlight using repeated calls to myRichTextBox.Text.IndexOf with the last found index + 1 as the start position.
  2. Highlight the found texts using the default RichTextBox capabilities.

Solution 3

You can use the Find method to find the startindex of your searched text:

int indexToText = myRichTextBox.Find(searchText);

You can then use this index and the Select method to select the text:

int endIndex = searchText.Length;
myRichTextBox.Select(indexToText , endIndex);
Share:
14,591
eMi
Author by

eMi

I'm called eMi. Currently I'm working as a Junior Software Engineer. Lets say I belong to the new & younger generation of programmers and as of that circumstance I'm still learning and gaining experience. Worked with /- experienced in: C, C#, .NET, WPF, Java, HTML, CSS, ASP.NET Webforms and also some basic know-how in ASP.NET MVC 3, JavaScript (jQuery), Android-Development (Xamarin MonoDroid), iOS-Development (Xamarin MonoTouch), MVVMCross, SQL, SQL Server Compact, Entity Framework, Regular Expressions (RegEx), Bash, (XML and XSLT Transformations) Stackoverflow is a great way to exchange knowledge!

Updated on June 04, 2022

Comments

  • eMi
    eMi almost 2 years

    I have a RichTextBox with -by example- this text:

    "This is my Text"
    

    Now I want to "search" the RichTextBox for a Text (String), by example:

    "Text"
    

    Now "Text" should be selected/highlighted (for each one) in the RichTextBox..

    There is something like:

    myRichTextBox.Select();
    

    but here I have to set a StartPosition and so on, but I want to search for String!

    How could I do this? (Searched stackoverflow, didn't find something similiar..)

  • eMi
    eMi over 12 years
    Yes, I want to highlight the found text.. Thank you 4 showing me, How I could achieve this.. but how does it looks like in code? (I'm quite new in C#)
  • eMi
    eMi over 12 years
    thx for your answer, this is also a possible way yeah, but how I meant to highlight, with select I cant select more than 1 Word in the rtb
  • eMi
    eMi over 12 years
    yes, it works only 4 one word.. But I want all words selected with ONE Button-Click.. thats the problem..
  • sikender
    sikender over 12 years
    you can implement it as whole word selected, it is helping code to you understand that how search function will work in rich text box.
  • eMi
    eMi over 12 years
    I knew before, how do I mark text in the richtextbox, but not how to mark more than one text at once
  • eMi
    eMi over 12 years
    ok I did it, I used a Timer.. Now I can select all this words calling the method from this code eact 100ms..
  • sikender
    sikender over 12 years
    Good. Keep in mind that, if you find any code snippet. It's not always that it's a copy-paste and your work is done. sometime you have to need patch work into that as make that code run like wise your functionality. Thanks. and good attempt.
  • Sqeaky
    Sqeaky almost 12 years
    @eMi Adding to what sikender said I have seen more than one malicious piece of code posted on the internet. I always rewrite using the concepts provided, so I can be certain what is put into the software is safe.
  • p0iz0neR
    p0iz0neR about 8 years
    This will search and select the text. When it reaches the end and not found anything it will go to start and select the first occurrence. This looks perfect.