making a simple search function, making the cursor jump to (or highlight) the word that is searched for
Asked Answered
E

3

5

I have now used way too long time, trying to figure out a problem, which I didn't think would be that hard.

Here is the deal:

I am writing a small application using C# and WPF.

I have a RichTextBox containing a FlowDocument.

I have added a small textbox and a button below my richtextbox.

The user then types in the word he/she wishes to search for, and presses the button.

The richtextbox will then jump to the first occurrance of that word.

it is enough that it just jumps to the correct line - it can also select, highlight or place the cursor by the word - anything will do, as long as the richTextBox is scrolled to the word.

Continuing to press the button, will then jump to the next occurance of the word, and so on so forth, till the end of the document.

As I said - I thought it to be a simple task - however I am having serious problems figuring this out.

Earley answered 18/11, 2009 at 15:36 Comment(0)
S
16

This should do the job:

public bool DoSearch(RichTextBox richTextBox, string searchText, bool searchNext)
{
  TextRange searchRange;

  // Get the range to search
  if(searchNext)
    searchRange = new TextRange(
        richTextBox.Selection.Start.GetPositionAtOffset(1),
        richTextBox.Document.ContentEnd);
  else
    searchRange = new TextRange(
        richTextBox.Document.ContentStart,
        richTextBox.Document.ContentEnd);

  // Do the search
  TextRange foundRange = FindTextInRange(searchRange, searchText);
  if(foundRange==null)
    return false;

  // Select the found range
  richTextBox.Selection.Select(foundRange.Start, foundRange.End);
  return true; 
}

public TextRange FindTextInRange(TextRange searchRange, string searchText)
{
  // Search the text with IndexOf
  int offset = searchRange.Text.IndexOf(searchText);
  if(offset<0)
    return null;  // Not found

  // Try to select the text as a contiguous range
  for(TextPointer start = searchRange.Start.GetPositionAtOffset(offset); start != searchRange.End; start = start.GetPositionAtOffset(1))
  {
    TextRange result = new TextRange(start, start.GetPositionAtOffset(searchText.Length);
    if(result.Text == searchText)
      return result;
  }
  return null;
}

The reason for the for() loop in FindTextInRangeUnfortunately the range.Text strips out non-text characters, so in some cases the offset computed by IndexOf will be slightly too low.

Scherer answered 18/11, 2009 at 17:5 Comment(5)
It worked like a charm. Thanks a million for your answer, buddy. You have no idea how much you helped me. Have a nice day!Earley
The first return in FindTextInRange should however he changed to a null, instead of a false :)Earley
Thanks. That's what happens when you type up an idea and don't bother to try it out. I edited false -> null it in my answer.Scherer
Years later and it's still useful! Thanks Ray, your FindTextInRange method saved me, and your for loop explanation was particularly helpful.Rosewater
Greetings; sorry for being a noob but; this wont work for me (yet). I can't get a reference for TextRange. Still I was assuming I could use: rtb_Document.Lenght as Range; but it seems not to be the case. As for TextPointer Im completely Clueless. In "my" answer bellow (now updated) i could actually perform the search and highlight but I am unable to: return to the 1st value found; nor retrieve the current result index (since result location is character value in text). Can you point me the way please?Pyongyang
P
2

I have used a Different Approach. Using a TextBox to Set the Keyword; this Searches for the KeyWord on Button Click. Finds the Keyword; Highlighs it and Focuses on that KeyWord.

    // Index of Current Result Found (Counts Characters not Lines or Results)
    private int IndexOfSearchResultFound;
    // Start Position Index of RichTextBox (Initiated as 0 : Beggining of Text / 1st Char)
    private int StartOfSelectedKeyword;
    private int EndOfSelectedKeyword;

    private void btnSearch_Click(object sender, EventArgs e)
    {
        // Reset Keyword Selection Index. (0 is the Staring Point of the Keyword Selection)
        IndexOfSearchResultFound = 0;

        // Specify the End of the Selected Keyword; using txt_Search.Text.Lenght (Char Ammount).
        EndOfSelectedKeyword = txt_Search.Text.Length;

        // If txt_Search.Text is not Empty
        if (txt_Search.Text.Length > 0)
        {
            // Find Keyword in RichTextBox.Text
            IndexOfSearchResultFound = FindKeyword(txt_Search.Text.Trim(), StartOfSelectedKeyword, rtb_Hosts.Text.Length);

            // If string was found in RichTextBox; Highlight it and Focus on Keyword Found Location
            if (IndexOfSearchResultFound >= 0)
            {
                // Focus on Currently Found Result
                rtb_Hosts.Focus();

                // Highlight the search string
                rtb_Hosts.Select(IndexOfSearchResultFound, EndOfSelectedKeyword);

                // Sets a new Starting Position (after the Position of the Last Result Found)
                // To be Ready to Focus on the Next Result
                StartOfSelectedKeyword = IndexOfSearchResultFound + EndOfSelectedKeyword;
            }
        }
    }


    private int FindKeyword(string _SearchKeyword, int _KeywordSelectionStart, int _KeywordSelectionEnd)
    {
        // De-Select Previous Searched String (Keyword)
        if (_KeywordSelectionStart > 0 && _KeywordSelectionEnd > 0 && IndexOfSearchResultFound >= 0)
        { rtb_Hosts.Undo(); }

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

        // A valid Starting index should be specified.
        // if indexOfSearchText = -1, Means that Search has reached the end of Document
        if (_KeywordSelectionStart >= 0 && IndexOfSearchResultFound >= 0)
        {
            // Find Keyword
            IndexOfSearchResultFound = rtb_Hosts.Find(_SearchKeyword, _KeywordSelectionStart, _KeywordSelectionEnd, RichTextBoxFinds.None);

            // Determine whether the text was found in richTextBox
            retVal = IndexOfSearchResultFound;
        }
        // Return the index to the specified Keyword text.
        return retVal;
    }

The only thing I couldn't achieve yet is to return to the 1st Search Result

Pyongyang answered 5/2, 2014 at 16:24 Comment(0)
R
0

This is a variant that will find the match closest to the Caret position.

  private TextRange FindText(string findText)
    {
      var fullText = DoGetAllText();
      if (string.IsNullOrEmpty(findText) || string.IsNullOrEmpty(fullText) || findText.Length > fullText.Length)
        return null;

      var textbox = GetTextbox();
      var leftPos = textbox.CaretPosition;
      var rightPos = textbox.CaretPosition;

      while (true)
      {
        var previous = leftPos.GetNextInsertionPosition(LogicalDirection.Backward);
        var next = rightPos.GetNextInsertionPosition(LogicalDirection.Forward);
        if (previous == null && next == null)
          return null; //can no longer move outward in either direction and text wasn't found

        if (previous != null)
          leftPos = previous;
        if (next != null)
          rightPos = next;

        var range = new TextRange(leftPos, rightPos);
        var offset = range.Text.IndexOf(findText, StringComparison.InvariantCultureIgnoreCase);
        if (offset < 0)
          continue; //text not found, continue to move outward

        //rtf has broken text indexes that often come up too low due to not considering hidden chars.  Increment up until we find the real position
        var findTextLower = findText.ToLower();
        var endOfDoc = textbox.Document.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
        for (var start = range.Start.GetPositionAtOffset(offset); start != endOfDoc; start = start.GetPositionAtOffset(1))
        {
          var result = new TextRange(start, start.GetPositionAtOffset(findText.Length));
          if (result.Text?.ToLower() == findTextLower)
          {
            return result;
          }
        }
      }
    }

If you want to highlight the match then it'd be as simple as changing this method to void and doing this when you found the match:

textbox.Selection.Select(result.Start, result.End);
Rationalism answered 28/10, 2017 at 6:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.