Howto: Use Text Frames

This chapter describes how you can control and manipulate text using text frames.

The source code for for the sample programs shown here is contained in the following directories:

  • %USERPROFILE%\My Documents\TX Text Control 29.0.NET for Windows Forms\Samples\WinForms\VB.NET\Text Frames
  • %USERPROFILE%\My Documents\TX Text Control 29.0.NET for Windows Forms\Samples\WinForms\CSharp\Text Frames

An overview of all properties, methods and events which can be used in conjunction with text frames can be found at Images and Text Frames.

Used TX Text Control controls:

  • TXTextControl.TextControl
  • TXTextControl.ButtonBar
  • TXTextControl.RulerBar
  • TXTextControl.StatusBar

Relevant API Links

Inserting a Text Frame

A text frame is inserted by first creating an instance of the TextFrame class, and then adding it to the TextFrameCollection:

TXTextControl.TextFrame newFrame = new TXTextControl.TextFrame(new Size(1000,1000));
textControl1.TextFrames.Add (newFrame, TXTextControl.HorizontalAlignment.Left,
    -1,TXTextControl.TextFrameInsertionMode.DisplaceText);
Dim NewFrame As New TXTextControl.TextFrame(New Size(1000, 1000))
TextControl1.TextFrames.Add(NewFrame, TXTextControl.HorizontalAlignment.Left, _
    -1, TXTextControl.TextFrameInsertionMode.DisplaceText)

Once a text frame has been inserted, it can be activated by clicking on its border. An activated text frame can be moved and resized by dragging its border with the mouse.

Text Control provides a built-in dialog box for changing the appearance of a text frame. The dialog box is displayed using the TextFrameAttributesDialog method:

textControl1.TextFrameAttributesDialog();
TextControl1.TextFrameAttributesDialog()

The menu item which displays this dialog box should be disabled when no text frame is selected. This can be done in the menu's popup event, using the GetItem method of the TextFrame collection:

mnuTextFrame_Properties.Enabled = (textControl1.TextFrames.GetItem() != null);
mnuTextFrame_Properties.Enabled = Not IsNothing(TextControl1.TextFrames.GetItem())

Printing Labels

This sample program shows how to insert multiple text frames simultaneously. The application is a label printing program, used to print, for instance, address labels or badges. The source code for this sample program can be found in the TextFrames\Printing Address Labels directory.

The sample program contains a dialog box for entering information about the label sheet to be used, e.g. the number of labels and their dimensions.

Image

The program then creates a text frame for each of the labels.

Image

The text frames can them be filled out, either manually by clicking on a field and typing in text, or from a file or database. When you click on the Fill in menu item, the sample program will enter the words "Address no." followed by the field identifier into each of the frames.

To load text from a file instead, simply replace the Selection.Text property with a call to the Selection.Load method.

private void mnuLabel_FillIn_Click(object sender, System.EventArgs e)
{
    int n = 0;
    foreach (TXTextControl.TextFrame frame in textControl1.TextFrames)
    {
        frame.Selection.Text = "Text Frame #" + Convert.ToString(n++);
    }
}
Private Sub mnuLabel_FillIn_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuLabel_FillIn.Click
    Dim N As Integer

    For Each Frame As TXTextControl.TextFrame In TextControl1.TextFrames
        Frame.Selection.Text = "Text Frame #" & N
        N = N + 1
    Next
End Sub