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.