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

New span in RadRichTextBox with different font, color, weight, style, family and size.

3 Answers 450 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Sarfraz
Top achievements
Rank 1
Sarfraz asked on 15 Sep 2011, 10:29 AM
Hi,
I have a RadRichTextBox on a usercontrol. When the user creates new record, the info in RadRichTextBox is written in Normal font style and black color. However, when the user later on reopens it to edit the record, the extra info added in RadRichTextBox is written in Bold Italic and Red color.

My code is following for the edit case:
//Span span = new Span("");  //Does not work
//Span span = new Span(" "); // Does not work
Span
 span = new Span(": "); // This works but adds this extra colon ":"
span.ForeColor = Colors.Red;
span.FontWeight = FontWeights.Bold;
span.FontStyle = FontStyles.Italic;
span.FontFamily = new FontFamily("Tahoma");
span.FontSize = Unit.PointToDip(10);
Paragraph paragraph = _radRichTextBox.Document.Sections.Last.Blocks.Last as Paragraph;
paragraph.Inlines.Add(span);
_radRichTextBox.UpdateEditorLayout();

There are two issues:
1) If I define a blank span or a span with only a spance in it, then the RadRichTextBox does not write the text with the new font changes (color, shape, size stays as the original one).
2) While typing, it adds one character at the end of the document (in that new span) but then it jumps to the beginning of the document in RadTextBox. Then we have to take the cursor again to the end to continue typing text.

Is there any way, the above issues could be resolved.

Thank you,
Sarfraz.
(Version: 2011.1.419.1040)

3 Answers, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 21 Sep 2011, 08:59 AM
Hello Sarfraz,

Adding a Span to the document directly through the Inlines property of a paragraph should only be done if you are programmatically creating a document and it has not been measured yet, i.e. it has not been shown in the editor before.
As for the current style when the user reopens the document, it can be set like this:

private void buttonSetEditingStyle_Click(object sender, RoutedEventArgs e)
{
    this.radRichTextBox.Document.CaretPosition.MoveToLastPositionInDocument();
 
    this.radRichTextBox.CurrentEditingStyle.SetPropertyValue(Span.ForeColorProperty, Colors.Red);
    this.radRichTextBox.CurrentEditingStyle.SetPropertyValue(Span.FontWeightProperty, FontWeights.Bold);
    this.radRichTextBox.CurrentEditingStyle.SetPropertyValue(Span.FontStyleProperty, FontStyles.Italic);
    this.radRichTextBox.CurrentEditingStyle.SetPropertyValue(Span.FontFamilyProperty, new FontFamily("Comic Sans MS"));
    this.radRichTextBox.CurrentEditingStyle.SetPropertyValue(Span.FontSizeProperty, Unit.PointToDip(10));
 
    this.radRichTextBox.Focus();
}

Don't hesitate to contact us if you have other questions.


Best wishes,

Iva
the Telerik team

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

0
Sarfraz
Top achievements
Rank 1
answered on 21 Sep 2011, 11:48 AM
Thanks for your reply.
This is almost exactly what I am looking for except that I don't want to write in red only at the end of the existing text but any where in the document.

Here is how I am doing it now:
            radRichTextBox.TextInputStart += new System.Windows.Input.TextCompositionEventHandler(radRichTextBox_TextInputStart);

and then:
        void radRichTextBox_TextInputStart(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            if (this.logType == LogType.Edit)
            {
                object color = radRichTextBox.CurrentEditingStyle.GetPropertyValue(Span.ForeColorProperty);
 
                if (!((Color)color == Colors.Red))
                {
                    radRichTextBox.CurrentEditingStyle.SetPropertyValue(Span.ForeColorProperty, Colors.Red);
                    radRichTextBox.CurrentEditingStyle.SetPropertyValue(Span.FontWeightProperty, FontWeights.Bold);
                    radRichTextBox.CurrentEditingStyle.SetPropertyValue(Span.FontStyleProperty, FontStyles.Italic);
                    radRichTextBox.CurrentEditingStyle.SetPropertyValue(Span.FontFamilyProperty, new FontFamily("Tahoma"));
                    radRichTextBox.CurrentEditingStyle.SetPropertyValue(Span.FontSizeProperty, Unit.PointToDip(10));
                    radRichTextBox.Focus();
                }
            }
        }

Regards,
Sarfraz

0
Boby
Telerik team
answered on 23 Sep 2011, 09:15 AM
Hi Sarfraz,
This is one possible solution; you can, however, try to set the default font style for the RadRichTextBox itself by using the DocumentInheritsDefaultStyleSettings property, as described here.

Don't hesitate to contact us if you have other questions.

Greetings,
Boby
the Telerik team

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

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