1

I need to do something like this Controlling the Length of a RichTextBox in C#

but in WPF :

if (richTextBox.Text.Length > maxsize)
            {
                // this method preserves the text colouring
                // find the first end-of-line past the endmarker

                Int32 endmarker = richTextBox.Text.IndexOf('\n', dropsize) + 1;
                if (endmarker < dropsize)
                    endmarker = dropsize;

                richTextBox.Select(0, endmarker);
                richTextBox.Cut();
            }

This throws compilation errors

Error 1 'System.Windows.Controls.RichTextBox' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Windows.Controls.RichTextBox' could be found (are you missing a using directive or an assembly reference?)

Error 2 No overload for method 'Select' takes 2 arguments

Apparently the Text property does not work in WPF, see this page RichTextBox (WPF) does not have string property “Text” with this question I found solution for the property Text but no for Select method.

 var myText = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

            if (myText.Text.Length > maxsize)
            {
                // this method preserves the text colouring
                // find the first end-of-line past the endmarker

                Int32 endmarker = myText.Text.IndexOf('\n', dropsize) + 1;
                if (endmarker < dropsize)
                    endmarker = dropsize;

                richTextBox.Select(0, endmarker);//No overload for method 'Select' takes 2 arguments
                myText.Select(0, endmarker);//No overload for method 'Select' takes 2 arguments
                console.Cut();
            }

So now, How do I achieve this in WPF.

Thanks in advance.

Community
  • 1
  • 1
Cyberguille
  • 1,552
  • 3
  • 28
  • 58
  • I've not used WPF before, but is it the `Document` property? [MSDN](https://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox_properties(v=vs.110).aspx) describes it as "Gets or sets the FlowDocument that represents the contents of the RichTextBox." – sab669 Jul 29 '15 at 12:24
  • @Ric I updated my question and see that solve the problem of property Text but not for Select method. – Cyberguille Jul 29 '15 at 13:40

1 Answers1

3

RichTextBox does not have Text property, what you are doing right now is the way to go, you can improve your code by moving it to an global extension method:

  public static class Extentions
    {
        public static string Text(this RichTextBox richTextBox)
        {
            TextRange content = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
            return content.Text;           
        }
    }

EDIT:
If your final goal is to select some text from the control, you wont need Text property at all:

For RichTextBox Selection you have to user TextPointer:

TextPointer text = richTextBox.Document.ContentStart;
while (text.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text)
{
    text = text.GetNextContextPosition(LogicalDirection.Forward);
}
TextPointer startPos = text.GetPositionAtOffset(0);
TextPointer endPos = text.GetPositionAtOffset(endmarker);
var textRange = new TextRange(startPos, endPos);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Blue));


EDIT 2: To append text to RichTextEditors.

private void processLine(string line)
{
    //You can clear if necessary
    //richTextBox.Document.Blocks.Clear();

    //whatever your logic. I'm only taking the first 10 chars
    string trimmed = text.Substring(0,9); 
    richTextBox.Document.Blocks.Add(new Paragraph(new Run(trimmed)));
}
E-Bat
  • 4,792
  • 1
  • 33
  • 58
  • I updated my question this is the solution for the property Text but no for Select method. – Cyberguille Jul 29 '15 at 13:30
  • Really I need trim text when it reaches a certain size.So GetPositionAtOffset and TextPointer is good idea for solve the problem of select some text. I don't understand the code of the while loop. To simplify and clarify I need a method that receives a text line and append this line at Text in RichTextbox and in case that the amount of data become too large, trim text when it reaches a certain size. – Cyberguille Jul 29 '15 at 16:00
  • So you are receiving texts from another source and, after verification, you want to append it to the RichTextBox? – E-Bat Jul 29 '15 at 18:21
  • Yes, This is What I want – Cyberguille Jul 29 '15 at 18:29
  • Ok, you can check now – E-Bat Jul 29 '15 at 19:18
  • I vote up because your answer help me , but not solve all my problem, I need remove the first lines – Cyberguille Jul 29 '15 at 19:59
  • Sure, that is what richTextBox.Document.Blocks.Clear() is for. – E-Bat Jul 29 '15 at 20:12
  • For Select method use yourRichTextBox.Selection.Text – Alina B. Jul 30 '15 at 06:44