# SubTextPartCollection.Add Method

> Adds a new subtextpart to the document. Its position is defined through the SubTextPart.Start and SubTextPart.Length properties. If the subtextpart's length is zero, the current text selection is used to define the position.

- **Product:** TX Text Control .NET for Windows Forms
- **Section:** Reference
- **Kind:** Method
- **Introduced:** 23.0
- **Namespace:** `TXTextControl`
- **Package:** `TXTextControl.TextControl.WinForms.SDK` (assembly `TXTextControl.dll`, from the private NuGet feed or Text Control Offline Packages)
- **.NET Framework:** reference `TXTextControl.dll` from the setup
- **Path:** TX Text Control .NET for Windows Forms > Reference > TXTextControl > SubTextPartCollection.Add Method
- **878 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/subtextpartcollection/add-method/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/subtextpartcollection/add-method/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/subtextpartcollection/add-method/llms-full.txt

---

### Description

Adds a new subtextpart to the document. Its position is defined through the [SubTextPart.Start](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/subtextpart/start-property/llms-full.txt) and [SubTextPart.Length](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/subtextpart/length-property/llms-full.txt) properties. If the subtextpart's length is zero, the current text selection is used to define the position.

### Syntax

```csharp
public SubTextPartCollection.AddResult Add(SubTextPart subTextPart);
```

```vbnet
Public Function Add(subTextPart As SubTextPart) As SubTextPartCollection.AddResult
```

### Parameters

| Parameter | Description |
|---|---|
| subTextPart | Specifies the subtextpart to add. |

### Return Value

The return value specifies whether the new textpart has successfully been inserted. It can be one of the [SubTextPartCollection.AddResult](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/subtextpartcollection/addresult-enumeration/llms-full.txt) values.

### Examples

The following example turns the selected customer name into a highlighted `SubTextPart` and checks the insertion result.

```csharp
textControl.Text = "Customer: Contoso Ltd.";
textControl.Select(10, 12);

SubTextPart customer = new("CustomerName", 10)
{
    HighlightColor = System.Drawing.Color.LightGoldenrodYellow,
    HighlightMode = HighlightMode.Always
};

SubTextPartCollection.AddResult result = textControl.SubTextParts.Add(customer);
if (result != SubTextPartCollection.AddResult.Successful)
{
    throw new InvalidOperationException($"The region could not be added: {result}");
}
```

```vbnet
textControl.Text = "Customer: Contoso Ltd."
textControl.Select(10, 12)

Dim customer As New SubTextPart("CustomerName", 10) With {
    .HighlightColor = System.Drawing.Color.LightGoldenrodYellow,
    .HighlightMode = HighlightMode.Always
}

Dim result As SubTextPartCollection.AddResult = textControl.SubTextParts.Add(customer)
If result <> SubTextPartCollection.AddResult.Successful Then
    Throw New InvalidOperationException($"The region could not be added: {result}")
End If
```

---

## Nearby

- **Previous:** [SubTextPartCollection Class](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/subtextpartcollection/llms-full.txt)
- **Next:** [SubTextPartCollection.AddResult Enumeration](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/subtextpartcollection/addresult-enumeration/llms-full.txt)
