# Creating and Configuring Controls in Code

> Create TextControl, ButtonBar, RulerBar, StatusBar, Ribbon and Sidebar in C# on a Windows Form: docking order, connections, and removing and disposing.

- **Product:** TX Text Control .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for Windows Forms > Integration Guide > Creating and Configuring Controls in Code
- **3075 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/controls-in-code/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/controls-in-code/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/controls-in-code/llms-full.txt

---

The `TextControl` and its bars are ordinary Windows Forms controls, so you can create them in code instead of drawing them in the designer. Forms that are built at run time need this, for example an editor that is added only when the user opens a document, or a ribbon whose tabs depend on the signed-in user. The examples create the editor, connect a button bar, rulers, a status bar, a ribbon and a sidebar, and set their properties, in the order that works.

The code goes into *Form1.cs* of a Windows Forms project with the package `TXTextControl.TextControl.WinForms.SDK`, as set up in [Create Your First Windows Forms Application](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-editor/first-application/llms-full.txt), but with an empty form.

### How Creating a Control in Code Works

A control created in code needs the same three things the designer gives it in *Form1.Designer.cs*:

1. **An instance**, created with `new`. The editor is a [TextControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/llms-full.txt), the bars are a [ButtonBar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/buttonbar/llms-full.txt), [RulerBar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/rulerbar/llms-full.txt) and [StatusBar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/statusbar/llms-full.txt), all in the namespace `TXTextControl`.
2. **A place on the form.** A control is shown only after it has been added to the `Controls` collection of the form or of a container, such as a `Panel`. Its `Dock` or `Anchor` property decides how it follows the size of the form.
3. **The connections.** The properties [ButtonBar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/buttonbar-property/llms-full.txt), [RulerBar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/rulerbar-property/llms-full.txt), [VerticalRulerBar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/verticalrulerbar-property/llms-full.txt) and [StatusBar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/statusbar-property/llms-full.txt) of the `TextControl` take the bar objects themselves. A property that is `null` means that no bar of this kind is connected; the editor works without it.

The four classes derive from `System.Windows.Forms.Control` and have its members, such as `Dock`, `Anchor`, `Size`, `Visible` and `Font`.

### Create the Editor and Its Bars

The following *Form1.cs* creates an editor with a button bar, a horizontal and a vertical ruler and a status bar, docks them so that they fill the form, and connects them:

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

public partial class Form1 : Form
{
    private TextControl textControl1;
    private ButtonBar buttonBar1;
    private RulerBar rulerBar1;
    private RulerBar rulerBar2;
    private StatusBar statusBar1;

    public Form1()
    {
        InitializeComponent();

        // Create the editor and its bars
        textControl1 = new TextControl();
        buttonBar1 = new ButtonBar();
        rulerBar1 = new RulerBar();
        rulerBar2 = new RulerBar();
        statusBar1 = new StatusBar();

        // Dock the bars at the edges and let the editor fill the rest
        buttonBar1.Dock = DockStyle.Top;
        rulerBar1.Dock = DockStyle.Top;
        rulerBar2.Dock = DockStyle.Left;
        statusBar1.Dock = DockStyle.Bottom;
        textControl1.Dock = DockStyle.Fill;

        // The control added last is docked first
        Controls.Add(textControl1);
        Controls.Add(rulerBar2);
        Controls.Add(rulerBar1);
        Controls.Add(buttonBar1);
        Controls.Add(statusBar1);

        // Connect the bars to the editor
        textControl1.ButtonBar = buttonBar1;
        textControl1.RulerBar = rulerBar1;
        textControl1.VerticalRulerBar = rulerBar2;
        textControl1.StatusBar = statusBar1;

        // Configure the editor
        textControl1.ViewMode = ViewMode.PageView;
        textControl1.ZoomFactor = 120;
    }
}
```

Keep the `namespace` Visual Studio created around the class. The code works as follows:

- **The order of `Controls.Add`.** Windows Forms docks the controls of a form from the end of the collection to its start. The status bar and the button bar, added last, take the bottom and the top edge first; the first ruler goes below the button bar, the vertical ruler to the left, and the editor, added first, fills the space that is left. With a different order, the rulers end up above the button bar or beside the status bar.
- **`rulerBar2`** becomes a vertical ruler because it is docked at the left and connected through `VerticalRulerBar`.
- **The connections** make the bars work with the editor: from then on, they follow its input position and change the formatting of its text.
- **[ViewMode](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/viewmode-property/llms-full.txt)** `PageView` shows pages like a printed document, and **[ZoomFactor](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/zoomfactor-property/llms-full.txt)** sets the zoom in percent.

Press **F5**: the form shows the same editor the designer would have built, with a working button bar, rulers and status bar. In the designer, that layout looks like this:

![A Windows Forms form in the designer with the button bar and the horizontal ruler at the top, the vertical ruler at the left, the page of textControl1 and the status bar with page, line and column numbers and a zoom slider at the bottom.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/designer-bars-rulers.f80b7136.webp)

### Add a Ribbon and a Sidebar

A ribbon is a [Ribbon](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms-ribbon/ribbon/llms-full.txt) control that holds tabs in its `Controls` collection, and a sidebar is a [Sidebar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms/sidebar/llms-full.txt). The following constructor builds an editor with the **Home** and **Insert** tabs and a styles sidebar at the right:

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

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        TextControl textControl1 = new TextControl() { Dock = DockStyle.Fill };

        // A ribbon with the Home and Insert tabs
        Ribbon ribbon1 = new Ribbon() { Dock = DockStyle.Top };
        ribbon1.Controls.Add(new RibbonFormattingTab() { Text = "Home" });
        ribbon1.Controls.Add(new RibbonInsertTab() { Text = "Insert" });

        // A styles sidebar at the right of the editor
        Sidebar sidebar1 = new Sidebar()
        {
            Dock = DockStyle.Right,
            ContentLayout = Sidebar.SidebarContentLayout.Styles
        };

        Controls.Add(textControl1);
        Controls.Add(sidebar1);
        Controls.Add(ribbon1);

        // Connect the ribbon and the sidebar to the editor
        textControl1.Ribbon = ribbon1;
        sidebar1.TextControl = textControl1;
    }
}
```

The code works as follows:

- [RibbonFormattingTab](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms-ribbon/ribbonformattingtab/llms-full.txt) and [RibbonInsertTab](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms-ribbon/ribboninserttab/llms-full.txt) are two of the predefined tabs. `Text` is the title on the tab.
- [TextControl.Ribbon](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/ribbon-property/llms-full.txt) connects the ribbon. The predefined tabs work with the `TextControl` of the ribbon that hosts them.
- [ContentLayout](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms/sidebar/contentlayout-property/llms-full.txt) chooses the panel the sidebar shows, and [Sidebar.TextControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms/sidebar/textcontrol-property/llms-full.txt) names the editor whose styles it lists. The sidebar is connected from its side, not through a property of the `TextControl`.

The ribbon is part of the Enterprise and Professional editions; see [Editions and Feature Availability](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/overview/editions/llms-full.txt). Tabs, groups and buttons of your own are created the same way; [Custom Tabs and Commands](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/custom-tabs/llms-full.txt) shows how.

### Change the Connections at Run Time

The connection properties can be set at any time. Set a property to `null` to disconnect a bar, and to another bar to replace it:

```csharp
// Hide the status bar and disconnect it from the editor
textControl1.StatusBar = null;
statusBar1.Visible = false;
```

A form with several editors uses this to share one set of bars; [Embedding the Editor in Forms and UserControls](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/embedding/#share-bars-between-several-editors) shows the complete code.

### Remove a Control Created in Code

A control removed from the form is not disposed of automatically. Remove it from the `Controls` collection and call `Dispose`, after disconnecting the bars that belong to it:

```csharp
// Disconnect the bars, then remove and dispose of the editor
textControl1.ButtonBar = null;
textControl1.StatusBar = null;
Controls.Remove(textControl1);
textControl1.Dispose();
```

Controls that are still on the form when it closes are disposed of together with the form.

### Next Steps

- [Visual Studio Designer Integration](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/designer/llms-full.txt): the same controls, built in the designer.
- [Embedding the Editor in Forms and UserControls](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/embedding/llms-full.txt): the editor in a user control and in tab pages.
- [TextControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/overview/components/textcontrol/llms-full.txt): the properties, methods and events of the editor.
- [Ribbon, Toolbars, Rulers, and Status Bar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/overview/components/ribbon-toolbars/llms-full.txt): the properties of each bar.

---

## Nearby

- **Previous:** [Visual Studio Designer Integration](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/designer/llms-full.txt)
- **Next:** [Embedding the Editor in Forms and UserControls](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/embedding/llms-full.txt)
