# Stylesheets

> Create, apply and update ParagraphStyle and InlineStyle in Windows Forms: the [Normal] style, the Styles gallery and sidebar, and styles copied from a template.

- **Product:** TX Text Control .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for Windows Forms > Document Editor > Stylesheets
- **3441 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/stylesheets/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/stylesheets/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/stylesheets/llms-full.txt

---

If the headings of a fifty-page report should become blue, formatting each heading by hand takes fifty edits; changing one style takes one. A *formatting style*, also called a stylesheet, is a named set of character and paragraph attributes that is stored in the document. Users apply styles from the ribbon, the Styles sidebar and the styles dialog box, and your code creates, applies, changes and copies them. Direct formatting without styles is described in [Text and Paragraph Formatting](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/formatting/llms-full.txt).

### What Styles Are

TX Text Control has two kinds of styles, both derived from [FormattingStyle](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/formattingstyle/llms-full.txt):

- **A paragraph style**, a [ParagraphStyle](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphstyle/llms-full.txt), formats whole paragraphs. Besides character attributes, it has a [ParagraphFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphstyle/paragraphformat-property/llms-full.txt) and a [ListFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphstyle/listformat-property/llms-full.txt), and [FollowingStyle](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphstyle/followingstyle-property/llms-full.txt) names the style the next paragraph gets when the user presses ENTER.
- **An inline style**, an [InlineStyle](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/inlinestyle/llms-full.txt), formats single words in a line. Text with an inline style keeps all attributes of its paragraph except the ones the inline style sets.

Every style is based on another style, and the chain ends at the root style `[Normal]`. A style inherits the attributes it doesn't set from its base style, which [BaseStyle](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/formattingstyle/basestyle-property/llms-full.txt) returns. Changing `[Normal]` therefore changes every style based on it. Since version 32.0, `[Normal]` can be changed, in Windows Forms through the `Font` property of the `TextControl`, at design time or at run time. The blog article [Sneak Peek 32.0: Modifying the Normal Stylesheet](https://www.textcontrol.com/blog/2023/07/04/sneak-peek-320-modifying-the-normal-stylesheet/) shows an example.

The styles of a document are in two collections of the `TextControl`: [ParagraphStyles](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/paragraphstyles-property/llms-full.txt), a [ParagraphStyleCollection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphstylecollection/llms-full.txt), and [InlineStyles](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/inlinestyles-property/llms-full.txt), an [InlineStyleCollection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/inlinestylecollection/llms-full.txt). They are saved and loaded with the document.

### Work With Styles in the Editor

The **Styles** group of the ribbon's formatting tab shows a gallery of the styles in the document. Each entry shows a preview, the name of the style and its base style:

![The Styles gallery of the ribbon, opened in an editor, listing [Normal], Normal, Title, Heading 1, Heading 2, Heading 3 and Intense Quote with a preview and "based on: [Normal]" for each style.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/styles-gallery-normal.4d450ca3.webp)

The **Styles** panel of the [Sidebar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms/sidebar/llms-full.txt) lists the same styles, with a preview when **Show Preview** is selected, and has a **Manage Styles...** button. Set [ContentLayout](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms/sidebar/contentlayout-property/llms-full.txt) of a sidebar to **Styles**, or connect a sidebar to the [StylesSidebar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms-ribbon/ribbonformattingtab/stylessidebar-property/llms-full.txt) property of the formatting tab, so that the dialog box launcher of the **Styles** group opens it:

![The Styles panel of the sidebar with the styles [Normal] and Style1 to Style7, each with a preview, and the Show Preview, Sort Alphabetical and Manage Styles buttons.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/styles-sidebar-show-preview.c2eff35c.webp)

[FormattingStylesDialog](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/formattingstylesdialog-method/llms-full.txt) opens the built-in dialog box for creating, deleting and modifying styles from your own menu. When a style is created or modified, a dialog box asks for its name, its base style, the style of the following paragraph and its type, and its **Font**, **Paragraph**, **Tabs**, **Numbering** and **Language** buttons open the formatting dialog boxes for the style:

![The Modify Style dialog box for Heading 1, based on [Normal], with Heading 1 as the style for the following paragraph, the style type Paragraph and the buttons Font, Paragraph, Tabs, Numbering and Language.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/modify-style-dialog.2f4b8957.webp)

### Create Styles in Code

Styles you create in code are added to the document in the editor. Creating and changing styles in code is part of the Enterprise edition; see [Editions and Feature Availability](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/overview/editions/llms-full.txt). The following override of `OnLoad` in *Form1.cs* adds a heading style, a body style and an inline style. It needs `using System.Drawing;` and `using TXTextControl;`:

```csharp
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    // A paragraph style based on [Normal]
    ParagraphStyle heading = new ParagraphStyle("Report Heading");
    heading.FontName = "Arial";
    heading.FontSize = 320;
    heading.Bold = true;
    heading.ForeColor = Color.DarkBlue;
    heading.ParagraphFormat.BottomDistance = 120;
    heading.ParagraphFormat.StructureLevel = 1;
    heading.FollowingStyle = "Report Body";

    ParagraphStyle body = new ParagraphStyle("Report Body");
    body.FontName = "Arial";
    body.FontSize = 220;
    body.ParagraphFormat.Alignment = TXTextControl.HorizontalAlignment.Justify;

    // A character style for single words
    InlineStyle highlight = new InlineStyle("Highlight");
    highlight.Bold = true;
    highlight.TextBackColor = Color.Yellow;

    textControl1.ParagraphStyles.Add(body);
    textControl1.ParagraphStyles.Add(heading);
    textControl1.InlineStyles.Add(highlight);
}
```

The code works as follows:

- **The constructor** with a name creates a style based on `[Normal]`. `new ParagraphStyle("Report Subheading", "Report Heading")` bases a style on another one instead.
- **The attributes** are the same as those of a selection, in twips: `FontSize = 320` is 16 points. The paragraph attributes are set on the style's `ParagraphFormat`.
- **The structure level** 1 makes paragraphs with the heading style entries of a table of contents; see [Tables of Contents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/tables-of-contents/llms-full.txt).
- **Following style.** After a heading, pressing ENTER continues with "Report Body".
- **Adding.** [Add](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphstylecollection/add-method/llms-full.txt) returns `false` if the style couldn't be added.

### Apply a Style

To format text with a style, assign its name to [Selection.FormattingStyle](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/formattingstyle-property/llms-full.txt). A paragraph style formats the paragraphs of the selection, and an inline style the selected characters:

```csharp
textControl1.Selection.FormattingStyle = "Report Heading";
```

[Paragraph.FormattingStyle](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraph/formattingstyle-property/llms-full.txt) reads or sets the paragraph style of one paragraph of the [Paragraphs](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/paragraphs-property/llms-full.txt) collection, including the styles that documents loaded from Word bring along, such as "Heading 1", and [RemoveInlineStyles](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/removeinlinestyles-method/llms-full.txt) removes the inline styles of the selection, so that it has the attributes of its paragraph style again.

### Change a Style Everywhere

[GetItem](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphstylecollection/getitem-method/llms-full.txt) returns a style by its name, or `null`. After changing it, call [Apply](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/formattingstyle/apply-method/llms-full.txt) to update all text formatted with it:

```csharp
private void redHeadingsButton_Click(object sender, EventArgs e)
{
    ParagraphStyle heading = textControl1.ParagraphStyles.GetItem("Report Heading");

    if (heading != null)
    {
        heading.ForeColor = Color.DarkRed;

        // Update all text formatted with the style
        heading.Apply();
    }
}
```

[Remove](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/formattingstylecollection/remove-method/llms-full.txt) deletes a style by its name, and enumerating the collections lists all styles with their [Name](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/formattingstyle/name-property/llms-full.txt) and `BaseStyle`.

### Copy Styles From a Template

A company template, for example a DOTX file, holds the styles that all documents should share. A [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/llms-full.txt) loads the template in the background, and the styles are copied into the editor. The constructor that takes a style creates a copy with all its attributes:

```csharp
private void ApplyTemplateStyles(string templatePath)
{
    using (ServerTextControl tx = new ServerTextControl())
    {
        tx.Create();
        tx.Load(templatePath, StreamType.WordprocessingML);

        foreach (ParagraphStyle style in tx.ParagraphStyles)
        {
            if (textControl1.ParagraphStyles.GetItem(style.Name) == null)
            {
                textControl1.ParagraphStyles.Add(new ParagraphStyle(style));
            }
        }

        foreach (InlineStyle style in tx.InlineStyles)
        {
            if (textControl1.InlineStyles.GetItem(style.Name) == null)
            {
                textControl1.InlineStyles.Add(new InlineStyle(style));
            }
        }
    }
}
```

Styles that already exist in the editor are kept. The blog article [Applying and Updating Formatting Styles from Master Templates (DOTX) in DOCX Documents](https://www.textcontrol.com/blog/2024/10/10/applying-and-updating-formatting-styles-from-master-templates-dotx-in-docx-documents/) also updates existing styles from the template and applies them.

### React to Style Changes

- [FormattingStyleListChanged](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/formattingstylelistchanged-event/llms-full.txt) occurs after styles have been added, removed or renamed, the moment to refill your own style list.
- [FormattingStyleChanged](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/formattingstylechanged-event/llms-full.txt) occurs after the attributes of styles have changed.
- [InputFormattingStyleChanged](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/inputformattingstylechanged-event/llms-full.txt) occurs when the input position moves to text with another style. [InputFormat.StyleName](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/inputformat/stylename-property/llms-full.txt) then returns the style at the input position.

In a protected document, [CanStyleFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/canstyleformat-property/llms-full.txt) tells whether styles may be applied. It depends on the edit mode and on [DocumentPermissions.AllowFormattingStyles](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/documentpermissions/allowformattingstyles-property/llms-full.txt); see [Document Protection and Editable Regions](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/protection/llms-full.txt).

### Next Steps

- [Bullets and Numbered Lists](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/lists/llms-full.txt): lists defined in paragraph styles.
- [Tables of Contents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/tables-of-contents/llms-full.txt): tables of contents built from styles with structure levels.
- [Sidebar and Panels](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/sidebar/llms-full.txt): the Styles panel and the other sidebar panels.
- [Formatting and Styling](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/mail-merge/styling/llms-full.txt): styles in mail merge templates.

---

## Nearby

- **Previous:** [Text and Paragraph Formatting](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/formatting/llms-full.txt)
- **Next:** [Bullets and Numbered Lists](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/lists/llms-full.txt)
