# Stylesheets

> Create, apply and change paragraph and character styles in VB6 with the Style object, the Styles collection and StyleDialog, and react to style changes.

- **Product:** TX Text Control ActiveX
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control ActiveX > Document Editor > Stylesheets
- **2909 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/stylesheets/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/stylesheets/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/stylesheets/llms-full.txt

---

A formatting style gives a set of character and paragraph attributes a name, such as *Headline*. Text formatted with a style changes whenever the style changes, so a document with styles gets a new look by editing a few styles instead of every paragraph. All styles of a document together are its stylesheet.

Visual Basic 6 developers who build a word processor on `TXTextControl` find here how styles work, how users create and change them in the built-in dialog box and how code applies, lists and changes styles through the `Style` object and the `Styles` collection.

### What Formatting Styles Are

`TXTextControl` knows two types of styles, and the [StyleType](https://docs.textcontrol.com/tx-text-control-activex/reference/style/styletype-property/llms-full.txt) property of a style tells which one it is:

- **Paragraph-based styles** (type 1) format complete paragraphs. They hold font attributes and paragraph attributes: alignment, indents, line spacing, frames, tabs and list settings.
- **Character-based styles** (type 2) format single characters inside a paragraph. They hold font attributes only. An attribute that a character-based style leaves open is inherited from the paragraph style: an empty `FontName`, a `FontSize` of 0 or a `FontBold` of 2 means "as in the paragraph".

A paragraph-based style also names the style that follows it. After the user presses the RETURN key, the new paragraph gets the style that [NextParaStyle](https://docs.textcontrol.com/tx-text-control-activex/reference/style/nextparastyle-property/llms-full.txt) gives, for example *Body Text* after a *Headline*.

In code, a style is a [Style](https://docs.textcontrol.com/tx-text-control-activex/reference/style/llms-full.txt) object, an OLE Automation object with a property for each attribute. The [Styles](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/styles-property/llms-full.txt) property of `TXTextControl` returns a [Styles](https://docs.textcontrol.com/tx-text-control-activex/reference/styles/llms-full.txt) collection with all styles of the document.

### Let Users Manage Styles

The built-in **Styles** dialog box lists the styles of the document. Its **New...** and **Modify...** buttons open the **New Style** or **Modify Style** dialog box, in which the user sets the name, the style it is based on, the style for the following paragraph and the style type. From there, the **Font...**, **Paragraph...**, **Tabs...**, **Numbering...** and **Language...** buttons open the dialog boxes for the attributes. **Delete** removes a style.

The [StyleDialog](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/styledialog-method/llms-full.txt) method opens the **Styles** dialog box. The *TXWords* sample calls it from the **Styles...** item of its **Format** menu:

```vbscript
' Call Text Control's built-in stylesheet dialog box
Private Sub mnuFormat_Styles_Click()
    TXTextControl1.StyleDialog
End Sub
```

`StyleDialog` returns `True` when the user has changed one or more styles and `False` when all styles remain unchanged.

![The built-in Styles dialog box of TXTextControl with the list of the document's styles and the New, Modify and Delete buttons.](https://docs.textcontrol.com/assets/img/tx-text-control-activex/styles-dialog.2515408c.webp)

![The built-in Modify Style dialog box of TXTextControl with the name, the base style, the style for the following paragraph, the style type and the Font, Paragraph, Tabs, Numbering and Language buttons.](https://docs.textcontrol.com/assets/img/tx-text-control-activex/modify-style-based-on.a284e549.webp)

The user applies a style with the formatting styles combo box of a connected [TXButtonBar](https://docs.textcontrol.com/tx-text-control-activex/reference/txbuttonbar/llms-full.txt). The [PosStyle](https://docs.textcontrol.com/tx-text-control-activex/reference/txbuttonbar/posstyle-property/llms-full.txt) property sets its position; *TXWords* places it first. [Button Bar, Ruler Bar and Status Bar](https://docs.textcontrol.com/tx-text-control-activex/document-editor/toolbars/llms-full.txt) describes the button bar.

### Apply a Style

[StyleCurrent](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/stylecurrent-property/llms-full.txt) returns the name of the style at the current input position, the position of the caret. Setting it applies a style: with text selected, all styles of the selected text change to the given style.

```vbscript
Private Sub mnuFormatHeadline_Click()
    If TXTextControl1.CanStyleFormat Then
        TXTextControl1.StyleCurrent = "Headline"
    End If
End Sub
```

The code works as follows:

- [CanStyleFormat](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/canstyleformat-property/llms-full.txt) tells whether the document can be formatted with styles. In a protected document, this depends on the permissions that [Document Protection](https://docs.textcontrol.com/tx-text-control-activex/document-editor/protection/llms-full.txt) describes.
- `StyleCurrent` takes the unique name of the style.

When the selection is formatted with several styles, `StyleCurrent` returns an empty string.

### List the Styles of a Document

The `Styles` collection works with `For Each`. The following procedure fills a Visual Basic `ComboBox` named `cboStyles` with the names of all styles:

```vbscript
Private Sub FillStyleList()
    Dim style As Object
    cboStyles.Clear
    For Each style In TXTextControl1.Styles
        cboStyles.AddItem style.StyleName
    Next
End Sub
```

The code works as follows:

- `TXTextControl1.Styles` returns the `Styles` collection. It is read only and available at run time only.
- Each member of the collection is a `Style` object, and [StyleName](https://docs.textcontrol.com/tx-text-control-activex/reference/style/stylename-property/llms-full.txt) returns its name.

[Count](https://docs.textcontrol.com/tx-text-control-activex/reference/styles/count-property/llms-full.txt) returns the number of styles in the collection.

### Change a Style in Code

[Item](https://docs.textcontrol.com/tx-text-control-activex/reference/styles/item-method/llms-full.txt) returns the `Style` object of a style by its name. After you have changed its properties, [Apply](https://docs.textcontrol.com/tx-text-control-activex/reference/style/apply-method/llms-full.txt) applies the new settings to the document:

```vbscript
Dim style As Object
Set style = TXTextControl1.Styles.Item("Headline")
style.FontName = "Arial"
style.FontSize = 400
style.FontBold = 1
style.Apply
```

The code works as follows:

- `Item("Headline")` returns the style named *Headline*. A name that matches no style raises an error.
- [FontName](https://docs.textcontrol.com/tx-text-control-activex/reference/style/fontname-property/llms-full.txt), [FontSize](https://docs.textcontrol.com/tx-text-control-activex/reference/style/fontsize-property/llms-full.txt) and [FontBold](https://docs.textcontrol.com/tx-text-control-activex/reference/style/fontbold-property/llms-full.txt) change the style's font. The style's `FontSize` is in twips, 1/1440 inch, so 400 is 20 points.
- `Apply` applies every setting made since the previous `Apply` call or since the object was created. All text formatted with *Headline* changes.

To change the style at the input position, pass `StyleCurrent` to `Item`: `Set style = TXTextControl1.Styles.Item(TXTextControl1.StyleCurrent)`.

#### Where Style Properties Differ

The `Style` object has most of the formatting properties of `TXTextControl`, with a few differences in units and values:

| Style property | Difference from TXTextControl |
|---|---|
| [FontSize](https://docs.textcontrol.com/tx-text-control-activex/reference/style/fontsize-property/llms-full.txt) | In twips instead of points. |
| [FontUnderline](https://docs.textcontrol.com/tx-text-control-activex/reference/style/fontunderline-property/llms-full.txt) | Combines the underline styles: &H1 single, &H2 double, &H4 words only, &H10 inherited from the paragraph style. |
| [Baseline](https://docs.textcontrol.com/tx-text-control-activex/reference/style/baseline-property/llms-full.txt), [BaselineType](https://docs.textcontrol.com/tx-text-control-activex/reference/style/baselinetype-property/llms-full.txt) | `BaselineType` decides whether `Baseline` is a superscript (4) or subscript (8) offset. |
| [Alignment](https://docs.textcontrol.com/tx-text-control-activex/reference/style/alignment-property/llms-full.txt) | Its values start at 1 for left-aligned text. |
| [TabPos](https://docs.textcontrol.com/tx-text-control-activex/reference/style/tabpos-property/llms-full.txt), [TabType](https://docs.textcontrol.com/tx-text-control-activex/reference/style/tabtype-property/llms-full.txt) | Take the tab index, 1 to 14, as a parameter: `style.TabPos(1) = 1440`. |

The list properties [ListType](https://docs.textcontrol.com/tx-text-control-activex/reference/style/listtype-property/llms-full.txt), [BulletAttribute](https://docs.textcontrol.com/tx-text-control-activex/reference/style/bulletattribute-property/llms-full.txt) and [NumberingAttribute](https://docs.textcontrol.com/tx-text-control-activex/reference/style/numberingattribute-property/llms-full.txt) take the same values as those of `TXTextControl`, which [Bulleted and Numbered Lists](https://docs.textcontrol.com/tx-text-control-activex/document-editor/lists/llms-full.txt) describes. Paragraph, tab and list settings work with paragraph-based styles only.

#### Create a Style

The `Styles` collection has no method that adds a style. Users create a new style with the **New...** button of the **Styles** dialog box that `StyleDialog` opens.

### React to Style Changes

Two events keep your own style list in step with the document:

- **[StyleChanged](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/stylechanged-event/llms-full.txt)** occurs when the input position has moved to text formatted with another style.
- **[StyleListChanged](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/stylelistchanged-event/llms-full.txt)** occurs when styles have been added or removed, for example in the dialog box that `StyleDialog` opens.

The following handlers keep the `cboStyles` combo box from the previous section up to date:

```vbscript
Private Sub TXTextControl1_StyleChanged()
    cboStyles.Text = TXTextControl1.StyleCurrent
End Sub

Private Sub TXTextControl1_StyleListChanged()
    FillStyleList
End Sub
```

### Styles in XML Documents

An XML document holds no formatting attributes. `TXTextControl` formats it with the styles of the CSS file that belongs to it, and in the XML editor mode, formatting is possible only through styles: the button bar changes the attributes of the current style, and the Styles dialog box and the `Styles` collection change the styles. In XML documents, `StyleName` is read only. [Editing XML Files](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/editing/llms-full.txt) describes the XML editor mode, and [Loading and Saving XML Files](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/load-save/llms-full.txt) saves changed styles to the CSS file.

### Key Members

| Member | Description |
|---|---|
| [StyleCurrent](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/stylecurrent-property/llms-full.txt) | Returns the style at the input position or applies a style to the selection. |
| [StyleDialog](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/styledialog-method/llms-full.txt) | Opens the built-in dialog box for creating, deleting and modifying styles. |
| [Styles](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/styles-property/llms-full.txt) | Returns the `Styles` collection with all styles of the document. |
| [Styles.Item](https://docs.textcontrol.com/tx-text-control-activex/reference/styles/item-method/llms-full.txt) | Returns a `Style` object by its name. |
| [Style.Apply](https://docs.textcontrol.com/tx-text-control-activex/reference/style/apply-method/llms-full.txt) | Applies the changed settings of a style to the document. |
| [Style.StyleType](https://docs.textcontrol.com/tx-text-control-activex/reference/style/styletype-property/llms-full.txt) | Returns whether a style is paragraph-based or character-based. |
| [Style.NextParaStyle](https://docs.textcontrol.com/tx-text-control-activex/reference/style/nextparastyle-property/llms-full.txt) | Sets the style of the paragraph that follows after RETURN. |
| [CanStyleFormat](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/canstyleformat-property/llms-full.txt) | Tells whether the document can be formatted with styles. |
| [StyleChanged](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/stylechanged-event/llms-full.txt), [StyleListChanged](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/stylelistchanged-event/llms-full.txt) | Occur when the style at the input position changes and when styles are added or removed. |

The programming categories [Stylesheet Formatting](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/stylesheet-formatting/llms-full.txt) and [Stylesheet Programming](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/stylesheet-programming/llms-full.txt) list all members, and [Style](https://docs.textcontrol.com/tx-text-control-activex/reference/style/llms-full.txt) lists every property of the `Style` object.

### Related Pages

- [Character and Paragraph Formatting](https://docs.textcontrol.com/tx-text-control-activex/document-editor/formatting/llms-full.txt): format text directly, without styles.
- [Bulleted and Numbered Lists](https://docs.textcontrol.com/tx-text-control-activex/document-editor/lists/llms-full.txt): list settings, which paragraph-based styles can hold.
- [Built-in Dialog Boxes](https://docs.textcontrol.com/tx-text-control-activex/document-editor/dialog-boxes/llms-full.txt): every dialog box that one method call opens.
- [Editing XML Files](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/editing/llms-full.txt): formatting XML documents through styles.

---

## Nearby

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