Modification of RichTextBox



  • Can part of the text already in RichTextBox, for example, change the colour of the text for this part of the text?



  • Sure. You only need one. https://msdn.microsoft.com/en-us/library/system.windows.documents.textpointer(v=vs.110).aspx to the right position of the text.

    Name TextPointer At the beginning of the text, let's share the symbols from the second to the fourth:

    var range = new TextRange(start.GetPositionAtOffset(2), start.GetPositionAtOffset(4));
    range.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Green);
    range.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Yellow);
    

    The question is how to get the beginning of the text. Here's the code:

    RichTextBox rtb = ...;
    FlowDocument doc = rtb.Document;
    TextPointer start = doc.ContentStart;
    

    I'm not quite sure, because doc.ContentStart starting point FlowDocumentYeah, he's coming. ParagraphAnd then the text. That's why it's better to do this:

    while (start != null &&
           start.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text)
        start = start.GetNextContextPosition(LogicalDirection.Forward);
    

    if (start == null)
    // в документе нет текста, выходим


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2