TX Text Control .NET for Windows Forms

Creating and Configuring Controls in Code

Topic

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, 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, the bars are a ButtonBar, RulerBar and StatusBar, 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, RulerBar, VerticalRulerBar and StatusBar 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:

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 PageView shows pages like a printed document, and ZoomFactor 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.

Add a Ribbon and a Sidebar

A ribbon is a Ribbon control that holds tabs in its Controls collection, and a sidebar is a Sidebar. The following constructor builds an editor with the Home and Insert tabs and a styles sidebar at the right:

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:

The ribbon is part of the Enterprise and Professional editions; see Editions and Feature Availability. Tabs, groups and buttons of your own are created the same way; Custom Tabs and Commands 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:

// 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 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:

// 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