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

How to get the current style

1 Answer 197 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 30 Jun 2016, 03:46 PM

I have a need to get the current style of a document. The GetCurrentSpanStyle method of RadRichTextBox is protected, so I cannot use this. The only way that I have been able to get the current style is to create a override of RichTextBoxCommandBase for this purpose.

public class GetCurrentSpanStyleCommand : RichTextBoxCommandBase
{
    public GetCurrentSpanStyleCommand(RadRichTextBox editor) : base(editor)
    {
    }
 
    protected override void ExecuteOverride(object parameter)
    {
 
    }
 
    public Span ExecuteGetCurrentSpanStyle()
    {
        return base.GetCurrentSpanStyle();
    }
}

Is there a better way to do this?

1 Answer, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 05 Jul 2016, 08:43 AM
Hello Chris,

The document could have many styles applied on its different document elements and I am not sure what exactly you need to access. I will list several of the options that might help you achieve the desired goal.

- The default style of the document you could obtain through the Style property of RadDocument:
StyleDefinition defaultDocumentStyle = this.radRichTextBox.Document.Style;

- You could check the style of the Span in which the caret is currently positioned:
StyleDefinition currentSpanStyle = this.radRichTextBox.Document.CaretPosition.GetCurrentSpanBox().AssociatedDocumentElement.Style;

- All the styles that are used in the document could be accessed through the StyleRepository. The next snippet demonstrates how you could obtain the StyleDefinition for the Normal style:
StyleDefinition normalStyle = this.radRichTextBox.Document.StyleRepository[RadDocumentDefaultStyles.NormalStyleName];

More information on working with styles in RadRichTextBox you could find in our documentation.

Hope this is helpful.

Regards,
Tanya
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
RichTextBox
Asked by
Chris
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Share this question
or