Howto: Edit and Format Text From Code

There are various applications which require text to be edited from program code. Report generators assemble documents from various smaller files and database fields. Web shops create order confirmations and send them out as HTML. Using TX Text Control, anything that can be done using the mouse and clicking menu items, can also be done from program code. The following chapters discuss some of the techniques involved.

The source code for this example is contained in the following directories:

  • %USERPROFILE%\My Documents\TX Text Control 29.0.NET for WPF\Samples\WPF\VB.NET\Editing from code
  • %USERPROFILE%\My Documents\TX Text Control 29.0.NET for WPF\Samples\WPF\CSharp\Editing from code

Used TX Text Control controls:

  • TXTextControl.TextControl

Relevant API Links

Selecting and Formatting Text

Text editing from code works similar to using mouse and menus. To change a word to bold, you would normally select it with the mouse, and then click on the Bold button. From code, you use a Text Control's Selection object to 'highlight' a word, and then set the Selection.Bold property.

textControl1.Text = "TX Text Control";
textControl1.Selection.Start = 3;
textControl1.Selection.Length = 4;
textControl1.Selection.Bold = true;
TextControl1.Text = "TX Text Control"
TextControl1.Selection.Start = 3
TextControl1.Selection.Length = 4
TextControl1.Selection.Bold = True

Image

Find and Replace

As an alternative to specifying start position and length of a word to be selected, the TextControl.Find method can be used to locate it. In this example, the font size of the word 'Text' is changed.

textControl1.Text = "TX Text Control";
textControl1.Find("Text");
textControl1.Selection.FontSize = 400;
TextControl1.Text = "TX Text Control"
TextControl1.Find("Text")
TextControl1.Selection.FontSize = 400

Image

It is also possible to replace a piece of text once it has been selected. Note that there is no dedicated Replace method for this purpose, you simply use the Selection.Text property instead.

Appending and Inserting Text Files

The first thing required in order to insert a file into your document is to specify the location where it is to be inserted. This is done using the Selection.Start property. Having set the input position to the desired location, the Selection.Load method can be used to insert a file, string or image. In this sample, first an RTF file is loaded, and then a HTML file is appended to its end.

TextControl1.Load("..\TestFiles\Caption.rtf", _
    TXTextControl.StreamType.RichTextFormat)
TextControl1.Selection.Start = TextControl1.Text.Length
TextControl1.Selection.Load("..\TestFiles\Net.htm", _
TXTextControl.StreamType.HTMLFormat)
textControl1.Load ("..\..\TestFiles\Caption.rtf",
    TXTextControl.StreamType.RichTextFormat);
textControl1.Selection.Start = textControl1.Text.Length;
textControl1.Selection.Load ("..\..\TestFiles\Net.htm",
TXTextControl.StreamType.HTMLFormat);

Moving to a Page, Line or Column

TX Text Control's InputPosition class informs you about which page, line and column the current input position is at. It can also be used to move the input position to a different location.

The following example shows how to create a Goto Page function. An instance of the InputPosition class is created and initialized with a previously entered page number. The new input position is then assigned to the TextControl.InputPosition property.

winGotoPage winGotoPage = new winGotoPage();
winGotoPage.ShowDialog();

if (winGotoPage.PageNo > 0 && winGotoPage.PageNo <= textControl1.Pages)
{
    TXTextControl.InputPosition InputPosition =
        new TXTextControl.InputPosition(winGotoPage.PageNo, 1, 0);
    textControl1.InputPosition = InputPosition;
}
else
{
    MessageBox.Show("Page number outside valid range.");
}
Dim winGotoPage As New winGotoPage()
winGotoPage.ShowDialog()

If winGotoPage.PageNo > 0 AndAlso winGotoPage.PageNo <= textControl1.Pages Then
    Dim InputPosition As New TXTextControl.InputPosition(winGotoPage.PageNo, 1, 0)
    textControl1.InputPosition = InputPosition
Else
    MessageBox.Show("Page number outside valid range.")
End If

Note that the InputPosition.Page property requires the ViewMode property to be set to PageView. In all other modes, text is displayed continuously as a single page, so the property will always have a value of 1.