TX Text Control .NET for Windows Forms

TX Text Control .NET for Windows Forms Tutorial

This tutorial shows you how to create a small word processor from scratch with just a few lines of code. It will be able to load and save files, use the clipboard, and will have dialog boxes for character and paragraph formatting, a ruler, a status bar and full keyboard and mouse interface.

Creating the Project

Start Visual Studio .NET and create a new project. Select either Visual Basic or C# as a project type, and Windows Application as a template.

Then use the Tools / Choose Toolbox Items... command to add TX Text Control to the toolbox. In the Customize Toolbox dialog, sort the list by namespace. Select TextControl, ButtonBar, StatusBar and RulerBar and confirm with OK.

After closing the dialog box, you will see four additional icons appear at the bottom of the toolbox, representing the Text Control and its Status Bar, Button Bar and Ruler Bar.

Creating the Controls

The next step is to put these four controls on to a form and connect them. Click on the ButtonBar icon and draw it on the form. In the same way, create a StatusBar, two RulerBar controls and finally a TextControl. Your form should now look like this:

Now click on each of the 5 controls in turn and set their Dock property, so that they fill the form entirely and are properly resized when the program runs. Set the Dock property to Top for the ButtonBar and one RulerBar, to Left for the second RulerBar and to Bottom for the StatusBar. Finally, set the Dock property of the TextControl to Fill.

At program start the keyboard input focus is set through the framework automatically to the control that has a tab index of zero. To be sure that the Text Control gets the input focus set its TabIndex property to zero. Hit F5 to run the program. If you resize the form, all 5 controls on it will be resized accordingly.

Connecting The Toolbars

Note that you can already type in the text window, but the toolbars do not yet work. In order to fix this, add the following code to the form's Load event procedure:

[C#] private void Form1_Load(object sender, System.EventArgs e) { textControl1.ButtonBar = buttonBar1; textControl1.RulerBar = rulerBar1; textControl1.VerticalRulerBar = rulerBar2; textControl1.StatusBar = statusBar1; }
[Visual Basic] Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load TextControl1.ButtonBar = ButtonBar1 TextControl1.RulerBar = RulerBar1 TextControl1.VerticalRulerBar = RulerBar2 TextControl1.StatusBar = StatusBar1 End Sub

Start the program again and have a look at the (now working) toolbars:

  • The Button Bar control on top of the window displays combo boxes and buttons for common word processing commands. The buttons to be displayed can be selected in the property window.
  • The Ruler Bar below it lets you set tabs and change paragraph indents. Click on the ruler to insert a new tab, or drag a tab away from the ruler to delete it. The type of a new tab is determined by the 4 tab buttons on the Button Bar.
  • The Status Bar at the bottom of the window displays page, line and column numbers.

Adding A Menu

In this chapter, we will add a menu to the text processor to enable you to call some of the built-in dialog boxes.

Drag a Main Menu control on your form and create a Format menu with the items Character... and Paragraph.... Name the items mnuFomat_Character and mnuFormat_Paragraph. (Please refer to the Visual Basic documentation, if you need help with creating menus).

Add the following code to the Click procedures of the menu items:

[C#] private void mnuFormat_Character_Click(object sender, System.EventArgs e) { textControl1.FontDialog(); } private void mnuFormat_Paragraph_Click(object sender, System.EventArgs e) { textControl1.ParagraphFormatDialog(); }
[Visual Basic] Private Sub mnuFormat_Character_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuFormat_Character.Click TextControl1.FontDialog() End Sub Private Sub mnuFormat_Paragraph_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuFormat_Paragraph.Click TextControl1.ParagraphFormatDialog() End Sub

Start the program again. You should be able to use the menu items to call the Font and Paragraph dialog boxes.

Now for the Edit menu. Create an Edit menu containing items for Cut, Copy and Paste. The code for these menu items is:

[C#] private void mnuCut_Click(object sender, System.EventArgs e) { textControl1.Cut(); } private void mnuCopy_Click(object sender, System.EventArgs e) { textControl1.Copy(); } private void mnuPaste_Click(object sender, System.EventArgs e) { textControl1.Paste(); }
[Visual Basic] Private Sub mnuEdit_Cut_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuEdit_Cut.Click TextControl1.Cut() End Sub Private Sub mnuEdit_Copy_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuEdit_Copy.Click TextControl1.Copy() End Sub Private Sub mnuEdit_Paste_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuEdit_Paste.Click TextControl1.Paste() End Sub

Having added these menu items, you can exchange formatted text with other word processors via the clipboard.

The last menu for now shall be a simple file menu. Create a File menu including the items Open File... and Save File As... and enter the following code:

[C#] private void mnuFile_OpenFile_Click(object sender, System.EventArgs e) { textControl1.Load(); } private void mnuFile_SaveFileAs_Click(object sender, System.EventArgs e) { textControl1.Save(); }
[Visual Basic] Private Sub mnuFile_OpenFile_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuFile_OpenFile.Click TextControl1.Load() End Sub Private Sub mnuFile_SaveFileAs_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuFile_SaveFileAs.Click TextControl1.Save() End Sub

The Load and Save methods, if called without a parameter as in the above example, will automatically open a dialog box for the user to select a filename and file format, and then load, respectively save, the selected file.

 
 
 

Products

Support

Downloads

Corporate

Buy Now