Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 465855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:25:11+00:00 2026-05-12T23:25:11+00:00

I have now used way too long time, trying to figure out a problem,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:25:11+00:00Added an answer on May 12, 2026 at 11:25 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used PyGame for a while now, and quite enjoyed doing so, but
I have used this forum for a while now to find answers to some
I have used sp_addlinkedserver to access the remote machines db now i am writing
I have used just Windows for programming so far. Now, I have an internship
I have used brow-leaves-67b10ed to render PDFs in UIView. Now my necessity is to
I have been tasked with refactoring some components that used xmlbeans to now make
I have never used Try-catch in my code before, but now I need to
I have been using Weblogic as my company used it. Now I want to
I used to have my permalinks to standard format, something like http://example.com/?page_id=2 . Now
Several of my ajax applications in the past have used GET request but now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.