# Text, Paragraphs, and Stylesheets

> Select, find and format text and paragraphs of a Windows Forms document in code, and read the paragraphs with their styles, lines and list numbers.

- **Product:** TX Text Control .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for Windows Forms > Document Processing > Text, Paragraphs, and Stylesheets
- **1887 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/text-paragraphs/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/text-paragraphs/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/text-paragraphs/llms-full.txt

---

Placeholders to replace, amounts to highlight, headings that should look alike in every document: most document processing works on text. Your code selects text by its position, finds and replaces it in every part of the document, formats characters and paragraphs, defines paragraph and inline styles and reads the text back. The sections below show how these tasks fit together in code and where each one is explained in full.

Formatting with the ribbon, the dialog boxes and in code is described in [Text and Paragraph Formatting](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/formatting/llms-full.txt), [Stylesheets](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/stylesheets/llms-full.txt) and [Bullets and Numbered Lists](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/lists/llms-full.txt); the same members work in a `ServerTextControl`. The examples work on the `TextControl` named `textControl1`, and *Form1.cs* needs these `using` directives:

```csharp
using System;
using System.Text;
using System.Windows.Forms;
using TXTextControl;
```

### Select and Format Characters

To format text, select it and set the properties of [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/selection-property/llms-full.txt), such as `Bold`, `FontSize` or `ForeColor`. [Select](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/select-method/llms-full.txt) takes the zero-based position of the first character and the number of characters, as [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, and [Format Characters](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/formatting/#format-characters) lists the character attributes. To insert text that is already formatted, assign a new `Selection` object, as [Creating Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/creating/#build-a-document-from-pieces) shows.

### Find and Replace Text

`TextControl.Find` searches only the text part with the input focus, so a placeholder in a header or a text frame is replaced only when your code searches every text part: [TextParts](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/textparts-property/llms-full.txt) contains them all, each with its own `Find` and `Selection`. [Replace All Occurrences](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/search-replace/#replace-all-occurrences) shows the replace loop and the `FindOptions`, and [Search Headers, Footers and Text Frames](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/search-replace/#search-headers-footers-and-text-frames) runs a search through all text parts. For templates that are filled with data, merge fields are the structured alternative to placeholder text; see [Mail Merge](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/mail-merge/llms-full.txt).

### Format Paragraphs

A paragraph ends with `\r\n`. [Paragraphs](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/paragraphs-property/llms-full.txt) contains the paragraphs of the text part with the input focus, numbered from 1, and [Paragraph.Format](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraph/format-property/llms-full.txt) takes a [ParagraphFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/llms-full.txt) object, so your code formats a paragraph by its number without selecting it:

```csharp
private void formatParagraphsButton_Click(object sender, EventArgs e)
{
    textControl1.ResetContents();
    textControl1.Selection.Text = "Terms and Conditions\r\nThese terms apply to all orders.";

    // Heading: centered and on the same page as the next paragraph
    textControl1.Paragraphs[1].Format = new ParagraphFormat(TXTextControl.HorizontalAlignment.Center)
    {
        KeepWithNext = true
    };
}
```

`HorizontalAlignment` exists in Windows Forms as well, so the code writes `TXTextControl.HorizontalAlignment`. [Format Paragraphs](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/formatting/#format-paragraphs) lists the properties of `ParagraphFormat`, and [Selection.ParagraphFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/paragraphformat-property/llms-full.txt) formats all paragraphs the selection touches.

### Use Paragraph and Inline Styles

A style gives a set of attributes a name. A [ParagraphStyle](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphstyle/llms-full.txt) applies to whole paragraphs and an [InlineStyle](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/inlinestyle/llms-full.txt) to words within a paragraph, and text that uses a style changes when the style changes. [Create Styles in Code](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/stylesheets/#create-styles-in-code), [Apply a Style](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/stylesheets/#apply-a-style) and [Change a Style Everywhere](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/stylesheets/#change-a-style-everywhere) show the code. 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).

### Create Bulleted and Numbered Lists

A [ListFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/listformat/llms-full.txt) assigned to `Paragraph.ListFormat` or `Selection.ListFormat` makes a paragraph a list item. [Bullets and Numbered Lists](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/lists/llms-full.txt) creates bulleted, numbered and multilevel lists, restarts the numbering and reads the list numbers.

### Read the Text of a Document

The following handler lists the paragraphs of the main text with their style, number of lines, alignment and list number:

```csharp
private void readTextButton_Click(object sender, EventArgs e)
{
    StringBuilder text = new StringBuilder();

    foreach (Paragraph paragraph in textControl1.TextParts.GetMainText().Paragraphs)
    {
        text.AppendLine($"#{paragraph.Number} \"{paragraph.FormattingStyle}\", " +
            $"{paragraph.Lines} line(s), {paragraph.Format.Alignment}: " +
            $"{paragraph.ListNumberText}{paragraph.Text}");
    }

    MessageBox.Show(text.ToString());
}
```

[Paragraph.Lines](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraph/lines-property/llms-full.txt) is the number of lines of a paragraph, [Text](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraph/text-property/llms-full.txt) its text and [ListNumberText](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraph/listnumbertext-property/llms-full.txt) its list number with the text around it. [TextControl.Lines](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/lines-property/llms-full.txt) gives the lines with the [Page](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/line/page-property/llms-full.txt) each one is on. `GetMainText` makes sure the handler reads the main text, even when the user's cursor is in a header.

### Next Steps

- [Tables and Calculations](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/tables/llms-full.txt): arrange text in rows and columns.
- [Fields and Subtext Parts](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/fields/llms-full.txt): mark text that your code finds and changes later.
- [Sections, Pages, Headers, and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/sections-headers/llms-full.txt): set the page format and repeat text on every page.
- [Stylesheets](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/stylesheets/llms-full.txt): let users apply and edit styles.

---

## Nearby

- **Previous:** [Saving and Converting Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/saving-converting/llms-full.txt)
- **Next:** [Tables and Calculations](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/tables/llms-full.txt)
