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

  • Home
  • SEARCH
  • 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 8479365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:00:09+00:00 2026-06-10T19:00:09+00:00

internal string Select(RichTextBox rtb, int index, int length) { TextRange textRange = new TextRange(rtb.Document.ContentStart,

  • 0
internal string Select(RichTextBox rtb, int index, int length)
    {
        TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);

        if (textRange.Text.Length >= (index + length))
        {
            TextPointer start = textRange.Start.GetPositionAtOffset(index, LogicalDirection.Forward);
            TextPointer end = textRange.Start.GetPositionAtOffset(index + length, LogicalDirection.Backward);
            rtb.Selection.Select(start, end); 
            rtb.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.LightBlue)); 
        }
        return rtb.Selection.Text;
    } 

whenever ApplyPropertyValue is called to change the backgroundcolor of selected text, it works great for the first time but it does not properly adjust the background color of the selected text segment on the second time it is called. I suspect this has to do with offsets of the document somehow being messed up after the function has been called.

What is a good way to fix this?

  • 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-06-10T19:00:11+00:00Added an answer on June 10, 2026 at 7:00 pm

    Try this (it needs a logic a bit more complex then yours), otherwise yes: you have problems with offsets!

    private static TextPointer GetTextPointAt(TextPointer from, int pos)
    {
            TextPointer ret = from;
            int i = 0;
    
            while ((i < pos) && (ret != null))
            {
                if ((ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.Text) || (ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.None))
                    i++;
    
                if (ret.GetPositionAtOffset(1, LogicalDirection.Forward) == null)
                    return ret;
    
                ret = ret.GetPositionAtOffset(1, LogicalDirection.Forward);
            }
    
            return ret;
    }
    
    internal string Select(RichTextBox rtb, int offset, int length, Color color)
    {
            // Get text selection:
            TextSelection textRange = rtb.Selection;
    
            // Get text starting point:
            TextPointer start = rtb.Document.ContentStart;
    
            // Get begin and end requested:
            TextPointer startPos = GetTextPointAt(start, offset);
            TextPointer endPos = GetTextPointAt(start, offset + length);
    
            // New selection of text:
            textRange.Select(startPos, endPos);
    
            // Apply property to the selection:
            textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
    
            // Return selection text:
            return rtb.Selection.Text;
    }
    

    And then use it in this way (I’m selecting from first character to the fifth in RED) :

    this.Select(this.myRichTextBox, 0, 5, Colors.Red);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Program { internal delegate int CallBack(int i); static void Main(string[] args) { CallBack
I have the following class, internal class PageInformation { public string Name { get;
I have method that is returning IQuerable given below: internal IQueryable<TradeLeads> GetLeadsByCategory(int categoryId) {
I'm trying to implement a MYSQL function MY_LEFT_STR(STRING x,INT position) in such a way
I have the following query (using jpa 2.0): String query = SELECT p.id, p.name
So i have this method: internal K GetValue<T, K>(T source, string col) where T
Given the method: internal static DataSet SelectDataSet(String commandText, DataBaseEnum dataBase) { var dataset =
Let's suppose if we have a class like: class Person { internal int PersonID;
I have this query be used on hibernate with java: String hql = select
What seems to be wrong with my hql query? String queryString = SELECT A.INITIAL,

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.