# Text and Paragraph Formatting

> Format characters and paragraphs in Windows Forms with Selection and ParagraphFormat: fonts, colors, indents, tab stops, formatting dialogs, CharFormatChanged.

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

---

Users format text in a TX Text Control editor the way they do in a word processor: they select it and click **Bold**, pick a font, center a paragraph or open the **Paragraph** dialog box. Your code has the same tools. It formats the selected text through `Selection`, sets paragraph attributes through `ParagraphFormat`, opens the built-in formatting dialog boxes and keeps your own toolbar in step with the text. Formatting through named styles has a page of its own, [Stylesheets](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/stylesheets/llms-full.txt), and bulleted and numbered lists are covered in [Bullets and Numbered Lists](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/lists/llms-full.txt).

### How Formatting Works

Formatting has two levels. *Character formatting*, such as the font, the size, bold or the text color, applies to single characters. *Paragraph formatting*, such as the alignment, the indents, the line spacing or tab stops, applies to whole paragraphs.

Both are set through [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/selection-property/llms-full.txt), which refers to the selected text of the text part with the input focus:

- **Text is selected.** A property such as `Bold` formats the selected characters. Paragraph attributes apply to every paragraph the selection touches.
- **Nothing is selected.** Character attributes apply at the input position, so the text the user types next gets them. Paragraph attributes apply to the paragraph of the input position.

Sizes and distances are measured in *twips*, a twentieth of a point: 20 twips are 1 point, 1,440 twips are 1 inch, and about 567 twips are 1 centimeter.

Users reach the same attributes through the **Font** and **Paragraph** groups of the ribbon's formatting tab, a [RibbonFormattingTab](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms-ribbon/ribbonformattingtab/llms-full.txt):

![The Home tab of the TX Text Control ribbon with the Clipboard, Font, Paragraph, Styles and Editing groups.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/ribbon-home-tab.31fd888c.webp)

A [ButtonBar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/buttonbar-property/llms-full.txt) and the mini toolbar that [ShowMiniToolbar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/showminitoolbar-property/llms-full.txt) turns on offer the common attributes too; [Toolbars and Mini Toolbars](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/toolbars/llms-full.txt) describes them. The text mini toolbar appears next to the selected text:

![The Windows Forms text mini toolbar above a table, with the font name and size, grow and shrink font, bold, italic, underline, text background and text color, list, indent, alignment and Styles buttons.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/minitoolbar-text-selection.418ee184.webp)

### Format Characters

The following `Click` handlers of two buttons on `Form1` toggle bold and format the selection as a heading. They need `using System.Drawing;` and `using TXTextControl;`:

```csharp
private void boldButton_Click(object sender, EventArgs e)
{
    // Toggle bold for the selected text or the input position
    textControl1.Selection.Bold = !textControl1.Selection.Bold;
}

private void headingButton_Click(object sender, EventArgs e)
{
    textControl1.Selection.FontName = "Arial";
    textControl1.Selection.FontSize = 320;   // 16 points, in twips
    textControl1.Selection.ForeColor = Color.DarkBlue;
    textControl1.Selection.Underline = FontUnderlineStyle.Single;
}
```

The character attributes of a [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/llms-full.txt):

| Property | Sets |
|---|---|
| [Bold](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/bold-property/llms-full.txt), [Italic](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/italic-property/llms-full.txt), [Strikeout](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/strikeout-property/llms-full.txt) | The font styles, as `true` or `false` |
| [Underline](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/underline-property/llms-full.txt) | `None`, `Single`, `Doubled`, `SingleWordsOnly` or `DoubledWordsOnly` |
| [FontName](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/fontname-property/llms-full.txt) | The font |
| [FontSize](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/fontsize-property/llms-full.txt) | The font size in twips; the default is 200 twips, 10 points |
| [ForeColor](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/forecolor-property/llms-full.txt), [TextBackColor](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/textbackcolor-property/llms-full.txt) | The text color and the background color of the text, as `System.Drawing.Color` |
| [Baseline](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/baseline-property/llms-full.txt) | A baseline shift in twips: positive values raise the text, negative values lower it |
| [AutoBaseline](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/autobaseline-property/llms-full.txt) | Automatic superscript or subscript, with the font scaled |
| [Capitals](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/capitals-property/llms-full.txt) | Lowercase letters shown as capitals, small capitals or petite capitals |
| [CharacterSpacing](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/characterspacing-property/llms-full.txt), [CharacterScaling](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/characterscaling-property/llms-full.txt) | Extra space per character in twips, and the character width in percent |

`AutoBaseline`, `Capitals`, `CharacterSpacing` and `CharacterScaling` were introduced in version 30.0.

### Format Text That Code Inserts

To format a range that your code has just inserted without selecting it on the screen, prepare a `Selection` object with a start position and a length, set its attributes and assign it to `textControl1.Selection`. [Select and Replace Text in Code](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/selections/#select-and-replace-text-in-code) shows the code and why the change then counts as one undo step.

### Format Paragraphs

Paragraph attributes are grouped in a [ParagraphFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/llms-full.txt) object. Create one, set the attributes you want to change and assign it to [Selection.ParagraphFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/paragraphformat-property/llms-full.txt):

```csharp
private void paragraphButton_Click(object sender, EventArgs e)
{
    ParagraphFormat format = new ParagraphFormat();
    format.Alignment = TXTextControl.HorizontalAlignment.Justify;
    format.LeftIndent = 567;       // 1 cm, in twips
    format.TopDistance = 120;      // 6 points before the paragraph
    format.BottomDistance = 120;   // 6 points after the paragraph
    format.LineSpacing = 150;      // 150 percent
    format.Frame = Frame.BottomLine;
    format.FrameLineWidth = 20;
    format.FrameStyle = TXTextControl.FrameStyle.Single;

    textControl1.Selection.ParagraphFormat = format;
}
```

`HorizontalAlignment` and `FrameStyle` also exist in `System.Windows.Forms`, which a Windows Forms file imports. Write them with the `TXTextControl` namespace, as above, or the compiler reports an ambiguous reference.

| Property | Sets |
|---|---|
| [Alignment](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/alignment-property/llms-full.txt) | `Left`, `Right`, `Center` or `Justify` |
| [LeftIndent](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/leftindent-property/llms-full.txt), [RightIndent](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/rightindent-property/llms-full.txt) | The indents in twips |
| [HangingIndent](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/hangingindent-property/llms-full.txt) | The hanging indent in twips; a negative value indents the first line instead |
| [TopDistance](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/topdistance-property/llms-full.txt), [BottomDistance](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/bottomdistance-property/llms-full.txt) | The space before and after the paragraph in twips |
| [LineSpacing](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/linespacing-property/llms-full.txt), [AbsoluteLineSpacing](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/absolutelinespacing-property/llms-full.txt) | The line spacing as a percentage of the font size, or in twips |
| [Frame](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/frame-property/llms-full.txt), [FrameStyle](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/framestyle-property/llms-full.txt), [FrameLineWidth](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/framelinewidth-property/llms-full.txt) | Frame lines around the paragraph: `LeftLine`, `RightLine`, `TopLine`, `BottomLine`, `Box` or `MergedBox`, single or double |
| [BackColor](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/backcolor-property/llms-full.txt) | The background color of the paragraph |
| [KeepLinesTogether](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/keeplinestogether-property/llms-full.txt), [KeepWithNext](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/keepwithnext-property/llms-full.txt), [PageBreakBefore](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/pagebreakbefore-property/llms-full.txt), [WidowOrphanLines](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/widoworphanlines-property/llms-full.txt) | Where page breaks may occur |
| [Direction](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/direction-property/llms-full.txt) | Left-to-right or right-to-left writing |
| [StructureLevel](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/structurelevel-property/llms-full.txt) | The level, 0 to 10, that a table of contents uses; 0 is body text |

[Tables of Contents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/tables-of-contents/llms-full.txt) explains structure levels, and [Right-to-Left UI](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/right-to-left/llms-full.txt) the writing direction.

### Set Tab Stops

Tab stops are three arrays of `ParagraphFormat` with one entry per tab: [TabPositions](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/tabpositions-property/llms-full.txt) in twips, [TabTypes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/tabtypes-property/llms-full.txt) and [TabLeaders](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/tableaders-property/llms-full.txt). A paragraph has at most 14 tabs, the value of [MaxTabs](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/maxtabs-field/llms-full.txt).

```csharp
private void tabsButton_Click(object sender, EventArgs e)
{
    ParagraphFormat format = new ParagraphFormat();
    format.TabPositions = new int[] { 2835, 5670 };                 // 5 cm and 10 cm
    format.TabTypes = new TabType[] { TabType.LeftTab, TabType.DecimalTab };
    format.TabLeaders = new TabLeader[] { TabLeader.None, TabLeader.Dot };

    textControl1.Selection.ParagraphFormat = format;
    textControl1.Selection.Text = "Item\t\t12.50";
}
```

A [TabType](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/tabtype/enumeration/llms-full.txt) is `LeftTab`, `RightTab`, `CenterTab`, `DecimalTab` or `RightBorderTab`; a decimal tab aligns the decimal sign at the position. A [TabLeader](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/tableader/enumeration/llms-full.txt) fills the gap with `Period`, `Dot`, `Dash` or `Underline`, or leaves it empty with `None`.

Users set the same values in the **Tabs** dialog box, which [TabDialog](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/tabdialog-method/llms-full.txt) opens: a list of the tab stop positions of the paragraph, the alignment and the leader of the selected tab stop, and **Set**, **Clear** and **Clear All** buttons:

![TX Text Control Words with the Tabs dialog box open over a table of contents: the tab stop position list, the Leader list open with None and three leader styles, the Right most tab option and the Set, Clear and Clear All buttons.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/tabs-dialog.a13c7557.webp)

### Use the One-Click Commands

`Selection` has methods for the commands of the ribbon buttons. Each works on the selection, or at the input position when nothing is selected:

- [GrowFont](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/growfont-method/llms-full.txt) and [ShrinkFont](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/shrinkfont-method/llms-full.txt) step the font size up and down through the sizes that the button bar and the font dialog box offer.
- [IncreaseIndent](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/increaseindent-method/llms-full.txt) and [DecreaseIndent](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/decreaseindent-method/llms-full.txt) change the left indent of the paragraphs, to positions taken from their tab positions and the indents of the surrounding paragraphs.
- [RemoveFormatting](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/removeformatting-method/llms-full.txt) resets the character attributes of the selection to their defaults, and the paragraph attributes too when nothing or more than one paragraph is selected.

### Open the Built-In Dialog Boxes

`TextControl` opens its formatting dialog boxes with one call each. Each method returns a `DialogResult`, which is `Cancel` when the user canceled the dialog box.

| Method | Dialog box |
|---|---|
| [FontDialog](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/fontdialog-method/llms-full.txt) | Font and character attributes. `FontDialog(1)` opens the tab for the baseline and further text effects. |
| [ParagraphFormatDialog](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/paragraphformatdialog-method/llms-full.txt) | Alignment, line spacing, indents and distances. `ParagraphFormatDialog(1)` opens the tab for frames and pagination. |
| [TabDialog](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/tabdialog-method/llms-full.txt) | Tab stops |
| [ForeColorDialog](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/forecolordialog-method/llms-full.txt), [TextBackColorDialog](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/textbackcolordialog-method/llms-full.txt) | The text color and the background color of the text |
| [LanguageDialog](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/languagedialog-method/llms-full.txt) | The language of the selected text, used for spell checking and hyphenation |

For example, a menu item that opens the paragraph settings calls `textControl1.ParagraphFormatDialog();` in its `Click` handler. [Built-in Dialog Boxes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/dialog-boxes/llms-full.txt) lists all dialog boxes of the editor.

### Keep Your Own Controls in Step

A toolbar of your own has to show the attributes at the input position, such as whether the **Bold** button is pressed. [CharFormatChanged](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/charformatchanged-event/llms-full.txt) occurs when the character attributes of the selection or the input position have changed, and [ParagraphFormatChanged](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/paragraphformatchanged-event/llms-full.txt) when the paragraph attributes have. [Integrating Existing Menus and Toolbars](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/menus-toolbars/#write-the-code) builds a `ToolStrip` whose buttons follow these events.

When the selection contains different values of an attribute, the `Selection` property returns its default value. [IsCommonValueSelected](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/iscommonvalueselected-method/llms-full.txt) with a [Selection.Attribute](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/attribute-enumeration/llms-full.txt) value, such as `Selection.Attribute.FontName`, tells you whether the whole selection has one value, so that a font box can stay empty for a selection in several fonts. [InputFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/inputformat-property/llms-full.txt) gives all attributes at the input position in one [InputFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/inputformat/llms-full.txt) object, whose properties are nullable, such as `bool?` for `Bold`.

### Next Steps

- [Stylesheets](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/stylesheets/llms-full.txt): name a set of attributes once and apply it everywhere.
- [Bullets and Numbered Lists](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/lists/llms-full.txt): format paragraphs as lists.
- [Toolbars and Mini Toolbars](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/toolbars/llms-full.txt): the formatting controls that come with the editor.
- [Text, Paragraphs, and Stylesheets](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/text-paragraphs/llms-full.txt): format documents without an editor.

---

## Nearby

- **Previous:** [Keyboard Shortcuts](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/keyboard-shortcuts/llms-full.txt)
- **Next:** [Stylesheets](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/stylesheets/llms-full.txt)
