This is a migrated thread and some comments may be shown as answers.

Clear RichTextBox

4 Answers 204 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 31 Mar 2011, 02:34 PM
What is the best way to clear a RichTextBox (Text, images, other...)?

4 Answers, 1 is accepted

Sort by
0
Accepted
Iva Toteva
Telerik team
answered on 31 Mar 2011, 05:12 PM
Hello Simon,

The easiest way would be to create a new document and assign it to the Document property of the rich text box:

this.radRichTextBox.Document = new RadDocument();

Another option (if you wish to be able to undo the operation) would be to select everything and delete it like this:
this.radRichTextBox.Document.Selection.SelectAll();
this.radRichTextBox.Document.Delete(false);

If you have other questions, do not hesitate to contact us again. 

Greetings,
Iva
the Telerik team
0
Simon
Top achievements
Rank 1
answered on 31 Mar 2011, 07:17 PM
Thanks you Iva,

I prefere option #2 as it will keep all your documents properties values.
0
Keith
Top achievements
Rank 1
answered on 15 Jun 2011, 06:46 PM
I have found for the second option that you should test to see if the Selection IsEmpty before trying to Delete it or you will get a stack overflow error:

someRichTextBox.Document.Selection.SelectAll();
if ( !someRichTextBox.Document.Selection.IsEmpty )
{
     someRichTextBox.Document.Delete(false);
}
0
Iva Toteva
Telerik team
answered on 20 Jun 2011, 05:04 PM
Hello Keith,

StackOverflowException can only occur if you are trying to delete the content of the document when it has not been measured and arranged. The document is automatically measured and arranged by Silverlight when it is shown in the editor, but if you want to perform the modification before that, you have to manually measure and arrange it in the following way:

this.radRichTextBox.Document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
this.radRichTextBox.Document.Arrange(new RectangleF(PointF.Empty, this.radRichTextBox.Document.DesiredSize));

We have revised this behavior and starting from the next release - the Q2 Beta, the document will be measured and arranged for you when you invoke an operation that requires that the document is measured.
If that does not help, we would appreciate a sample demo illustrating the behavior you are observing.

Kind regards,
Iva
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
RichTextBox
Asked by
Simon
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Simon
Top achievements
Rank 1
Keith
Top achievements
Rank 1
Share this question
or