Back to Devexpress

How To: Create New Linked Style

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

latest2.7 KB
Original Source

How To: Create New Linked Style

  • Dec 12, 2022
  • 2 minutes to read

This example explains how to create a linked style – a style that can be applied both to the character and paragraph, depending on the range to which it is applied.

The following code snippet demonstrates how to create linked styles.

View Example

csharp
document.BeginUpdate();
document.AppendText("Line One\nLine Two\nLine Three");
document.EndUpdate();

// Create new paragraph style
document.BeginUpdate();
ParagraphStyle lstyle = document.ParagraphStyles.CreateNew();
lstyle.Name = "MyLinkedStyle";
lstyle.LineSpacingType = ParagraphLineSpacing.Double;
lstyle.Alignment = ParagraphAlignment.Center;
document.ParagraphStyles.Add(lstyle);

// Create new character style
CharacterStyle lcstyle = document.CharacterStyles.CreateNew();
lcstyle.Name = "MyLinkedCStyle";
document.CharacterStyles.Add(lcstyle);

// Link this style to the paragraph style
lcstyle.LinkedStyle = lstyle;

lcstyle.ForeColor = System.Drawing.Color.DarkGreen;
lcstyle.Strikeout = StrikeoutType.Single;
lcstyle.FontSize = 24;
document.EndUpdate();

// Apply created styles 
// to the text range and to the entire paragraph
document.Paragraphs[1].Style = lstyle;

DocumentRange myRange = document.Paragraphs[0].Range;
CharacterProperties charProps = document.BeginUpdateCharacters(myRange);
charProps.Style = lcstyle;
document.EndUpdateCharacters(charProps);
vb
document.BeginUpdate()
document.AppendText("Line One" & vbLf & "Line Two" & vbLf & "Line Three")
document.EndUpdate()

' Create new paragraph style
Dim lstyle As ParagraphStyle = document.ParagraphStyles("MyLinkedStyle")
If lstyle Is Nothing Then
    document.BeginUpdate()
    lstyle = document.ParagraphStyles.CreateNew()
    lstyle.Name = "MyLinkedStyle"
    lstyle.LineSpacingType = ParagraphLineSpacing.Double
    lstyle.Alignment = ParagraphAlignment.Center
    document.ParagraphStyles.Add(lstyle)

    Dim lcstyle As CharacterStyle = document.CharacterStyles.CreateNew()
    lcstyle.Name = "MyLinkedCStyle"
    document.CharacterStyles.Add(lcstyle)
    lcstyle.LinkedStyle = lstyle

    lcstyle.ForeColor = System.Drawing.Color.DarkGreen
    lcstyle.Strikeout = StrikeoutType.Single
    lcstyle.FontSize = 24
    document.EndUpdate()

    ' Apply created styles 
    ' to the text range and to the entire paragraph
    document.Paragraphs(1).Style = lstyle

    Dim myRange As DocumentRange = document.Paragraphs(0).Range
    Dim charProps As CharacterProperties = document.BeginUpdateCharacters(myRange)
    charProps.Style = lcstyle
    document.EndUpdateCharacters(charProps)
End If