Back to Devexpress

How To: Create New Character Style

windowsforms-116609-controls-and-libraries-rich-text-editor-examples-styles-how-to-create-new-character-style.md

latest2.0 KB
Original Source

How To: Create New Character Style

  • Dec 12, 2022

The following example illustrates how to create a character style in code.

The following code snippet creates a new character style, specifies style settings, adds it to the document style collection and applies the style to the specified range.

View Example

csharp
richEditControl.Document.LoadDocument("Grimm.docx");

// Create a new style
CharacterStyle cstyle = richEditControl.Document.CharacterStyles.CreateNew();

// Specify style parameters
cstyle.Name = "MyCStyle";
cstyle.Parent = richEditControl.Document.CharacterStyles["Default Paragraph Font"];
cstyle.ForeColor = System.Drawing.Color.DarkOrange;
cstyle.Strikeout = StrikeoutType.Double;
cstyle.FontName = "Verdana";

// Add style to the collection
document.CharacterStyles.Add(cstyle);

// Obtain the second document paragraph
DocumentRange myRange = richEditControl.Document.Paragraphs[0].Range;

// Apply created style to the paragraph
CharacterProperties charProps =
    richEditControl.Document.BeginUpdateCharacters(myRange);
charProps.Style = cstyle;
richEditControl.Document.EndUpdateCharacters(charProps);
vb
Private Sub SurroundingSub()
    richEditControl.Document.LoadDocument("Grimm.docx")
    Dim cstyle As CharacterStyle = richEditControl.Document.CharacterStyles.CreateNew()
    cstyle.Name = "MyCStyle"
    cstyle.Parent = richEditControl.Document.CharacterStyles("Default Paragraph Font")
    cstyle.ForeColor = System.Drawing.Color.DarkOrange
    cstyle.Strikeout = StrikeoutType.Double
    cstyle.FontName = "Verdana"
    document.CharacterStyles.Add(cstyle)
    Dim myRange As DocumentRange = richEditControl.Document.Paragraphs(0).Range
    Dim charProps As CharacterProperties = richEditControl.Document.BeginUpdateCharacters(myRange)
    charProps.Style = cstyle
    richEditControl.Document.EndUpdateCharacters(charProps)
End Sub