Cyberguille
Cyberguille

Reputation: 1602

Controlling the Length of a RichTextBox in wpf C#

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.

Upvotes: 1

Views: 3603

Answers (1)

E-Bat
E-Bat

Reputation: 4892

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)));
}

Upvotes: 3

Related Questions