I have two questions relating to the contents of a RadRichTextBox, maybe they belong in different threads but I figure I'll save some time and try to post them both here.
1. What is the best way to clear the contents of the RadRichTextBox? I have tried recreating the associated document which does clear the contents but I get this weird issue where the control seems to create a large left and top margin. The code is easily reproducable with:
this
.TextBox.Document =
new
RadDocument();
I have also tried clearing the sections within document but this has no effect.
2. A common piece of input validation is to check that a control is not empty. Is there an easy way to do this or will I need to write an extension method for the chosen format provider to do this? For example I am using the XAML provider which contains alot of metadata about the document and various sections, but the user may not have entered any 'real' data, or just whitespaces and I need a way to pick this up.
Stephen
10 Answers, 1 is accepted
1. The way you have discovered for clearing the contents of a RadRichTextBox is indeed correct. The reason you're seeing large margins is because the default ones are optimized for printing. I'd recommend you to create a method to instantiate a new RadDocument for you and initialize it with your preferred settings. Another option would be to clear all the paragraphs in all the sections of the document (not all the sections since the margin is an attribute of the section), but I'd recommend the former.
2. In any case, you'll have to implement this kind of checks yourself. There is no API since "real data" definition varies. One way would be to use the TxtFormatProvider to export the document to plain text and verify that against whatever checks you want. There might be others, but those will be much trickier and generally unnecessary.
Don't hesitate to get back if you need any further assistance.
Kind regards,
Ivailo
the Telerik team
In regards to clearing the text box. How do I use the settings that it initially started with? The reason being what I consider the 'default' document settings when a RadRichTextBox control is loaded is perfect to my needs. I guess I could do a comparison or a deep copy of the document, but there must be some standard settings that are initialized in the RadRichTextBox that are not part of the RadDocument's default constructor initialization.
Stephen
The RadDocument loaded by the RadRichTextBox is has indeed slightly different default settings than if you create a new instance of the RadDocument manually. Here is a method that setups RadDocument with the same settings as the rich text box:
private
RadDocument CreateDocument()
{
RadDocument doc =
new
RadDocument();
doc.ParagraphDefaultSpacingAfter = 0;
doc.PageViewMargin =
new
SizeF(0, 0);
doc.SectionDefaultPageMargin =
new
Padding(6);
return
doc;
}
Please, get back to use if you need further help. Sincerely yours,
Alex
the Telerik team
This initialize method does not work, content of the document is not saved to the file system (when saving file manually).
Any other solutions? :)
tnx, Vedran
Can you please try to give us more details about the problems you experience?
Kind regards,
Mike
the Telerik team
The saved document simply has no content; it's blank.
I used your initialization method, but the result is same.
I cannot neither open that document again by using RadRichTextBox.
I worked it out by making an empty RTF document (eg. using MS Word) and using it as a template for creating new documents (what is pretty bad programming practice).
Tnx, Vedran
I am not quite able to understand your case.
The code form Alex's snippet shows how you can create a new blank document that has the same properties that RadRichTextBox sets on its default document. After you create this document, you can set it to the Document property of RadRichTextBox in the following way:
this
.radRichTextBox1.Document = CreateDocument();
Saving the document using the format providers is an entirely new story. If you wish to save the document, without showing it in a rich text box, you need to measure and arrange the document in advance. Please refer to the code-snippet below:
RadDocument document =
new
RadDocument();
//Set some properties, add Blocks (Paragraphs and Tables), etc.
MeasureAndArrangeInDefaultSize(document);
//Export
private
void
MeasureAndArrangeInDefaultSize(RadDocument document)
{
document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
document.Arrange(
new
RectangleF(PointF.Empty, document.DesiredSize));
}
If this does not answer your question, we would really appreciate some more detailed information on your scenario. Regards,
Iva
the Telerik team
In the latest versions of the control (starting from the Q2 official release - 2011.2.712) we have decided to invoke these operations internally whenever needed. Therefore, it will not be necessary to invoke them on your end.
If you have found a case when they need to be invoked explicitly as in the snippet, we would appreciate your feedback in order to fix the lapse.
Iva Toteva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>