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

Change default font or clear sections

2 Answers 292 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Joel
Top achievements
Rank 1
Joel asked on 26 May 2011, 09:06 PM
Hi,

I'm fairly new to playing with the RadRichtextBox and am wondering if anyone would know how to get around this problem.  I am programatically formatting font and then adding text to a Rich Text Box like so (richTB is a RichTextBox decalred in the XAML):

richTB.DocumentInheritsDefaultStyleSettings = true;
richTB.FontSize = 11;
 
Section sec = new Section();
 
Span sp = new Span();
sp.Text = "Some Text I want to write to RTB";
 
Paragraph par = new Paragraph();
par.Inlines.Add(sp);
 
sec.Blocks.Add(par);
 
richTB.Document.Sections.AddBefore(rtb.Document.Sections.First, sec);

Now, this works fine, the text appears at the start of the RichTextBox.  My problem is that if I move the cursor to the next line, the font changes to something bigger (default font size of original Section).  I've tried to remove all section of the document using:

richTB.Document.Sections.Clear();

Before writing the text I want to write to the RichTextBox but this call causes an exception (Object reference not set to an instance of an object.). Why can't I clear the collection? 

Is there something wrong with removing sections?  If there is, is there a way I could just change the font of the original section to use what I defined for the RadRichTextBox?  That is my real problem.

Thanks,

-- Joel



2 Answers, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 01 Jun 2011, 07:41 AM
Hello Joel,

Setting a default font size to RadRichTextBox is normally done in the following way:

<telerik:RadRichTextBox Name="editor" Grid.Row="1" FontSize="16" DocumentInheritsDefaultStyleSettings="True"/>

However, the FontSize is treated as set in dip, whereas the SelectionMiniToolBar and the RadRichTextBoxRibbonUI show the values in points (and the value they show will be 12, as 12 pt = 16 dip). In code-behind, you can make us of the converter methods of the Telerik.Windows.Documents.Model.Unit class in the following way:
this.radRichTextBox.FontSize = Unit.PointToDip(12);
// equivalent to
// this.radRichTextBox.FontSize = 16;

However, if you are importing a document from XAML, RTF or any other rich text format that is supported, these settings will not be applied to the paragraphs and spans that have these properties already set.

Currently only one section in the document is supported and removing a section or clearing all sections from a document currently edited in RadRichTextBox is problematic, as the document structure would become invalid if the section is removed.

If this is not the answer you were searching for, could you elaborate a bit more on your scenario and the behavior you are observing?


Greetings,
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
Joel
Top achievements
Rank 1
answered on 01 Jun 2011, 03:31 PM
Iva,

Found my error.  

Thanks,

-- Joel


Tags
RichTextBox
Asked by
Joel
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Joel
Top achievements
Rank 1
Share this question
or