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

Contents of the RadRichTextBox

10 Answers 307 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Stephen asked on 16 Sep 2010, 04:39 AM
Hi,

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

Sort by
0
Ivailo Karamanolev
Telerik team
answered on 16 Sep 2010, 07:16 AM
Hi Stephen,

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
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
0
Stephen
Top achievements
Rank 1
answered on 16 Sep 2010, 08:27 AM
Hi Ivailo thanks for the response.

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
0
Accepted
Alex
Telerik team
answered on 22 Sep 2010, 08:15 AM
Hello 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
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
0
Vedran
Top achievements
Rank 1
answered on 14 Apr 2011, 10:08 AM
Hi,
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
0
Mike
Telerik team
answered on 14 Apr 2011, 02:05 PM
Hello Vedran,

Can you please try to give us more details about the problems you experience?

Kind regards,
Mike
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
0
Vedran
Top achievements
Rank 1
answered on 10 May 2011, 12:34 PM
Hi,
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
0
Iva Toteva
Telerik team
answered on 13 May 2011, 08:28 AM
Hello 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
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
0
Vedran
Top achievements
Rank 1
answered on 18 May 2011, 04:10 PM
Yeah, magic words... measure & arrange :)) tnx!
0
Dave
Top achievements
Rank 1
answered on 31 Oct 2011, 04:16 PM
sigh... nevermind
0
Iva Toteva
Telerik team
answered on 01 Nov 2011, 03:58 PM
Hi Dave,

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.

Regards,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
RichTextBox
Asked by
Stephen
Top achievements
Rank 1
Answers by
Ivailo Karamanolev
Telerik team
Stephen
Top achievements
Rank 1
Alex
Telerik team
Vedran
Top achievements
Rank 1
Mike
Telerik team
Iva Toteva
Telerik team
Dave
Top achievements
Rank 1
Share this question
or