Products Technologies Demo Docs Blog Support Company

InputPosition, TextChars and Selection: Various Ways to Manipulate Text and the Input Position

TX Text Control provides many ways to set the input position, to manipulate text, and to retrieve text at the current input position. This article gives an overview of various API members and their typical applications.

InputPosition, TextChars and Selection: Various Ways to Manipulate Text and the Input Position

Text can be manipulated in many ways using the TX Text Control API. Depending on the current task, various classes can be used to set the input position, to retrieve the text or to set the text at the current input position.

The Basics: The Input Position

The following two properties can be used to manipulate the input position:

  • Selection class

  • InputPosition class

Both classes can be use to set the input position to a specific character index which is 0-based.

TX Text Control Input Position

Selection Class

The following code uses the Selection class to set the input position to the index location 3 which is the beginning of character location 4.

textControl1.Selection.Start = 3;

The Selection class can be now used to select a range of text in order to replace it with new text.

textControl1.Selection.Start = 3;
textControl1.Selection.Length = 4;
textControl1.Selection.Text = "New Text";

Selecting Text

Although the above code works, the Selection class should be used in a different way. The Selection property accepts a complete Selection object. The object can be prepared and then applied to the text as shown in the code snippet below.

textControl1.Text = "TX Text Control";

TXTextControl.Selection selection = new TXTextControl.Selection(3, 4) {
 Text = "New Text"
};

textControl1.Selection = selection;

InputPosition Class

The InputPosition constructor offers other possibilities to set the input position. This class can be used to set the input position in case the X and Y coordinates are known or a specific page, column and row number is used. The following implementations of the constructor are available:

public InputPosition(int page, int line, int column);
public InputPosition(int textPosition);
public InputPosition(int textPosition, TextFieldPosition textFieldPosition);
public InputPosition(Point location);

To set the input position to index 3 similar to the above sample using the Selection property, the following code is required.

textControl1.InputPosition = new TXTextControl.InputPosition(3);

Retrieving Text

In order to retrieve text from a specific range of text, the Selection class must be used. The following code shows how to retrieve the string "Text" from the above sample.

textControl1.Text = "TX Text Control";
 TXTextControl.Selection selection = new TXTextControl.Selection(3, 4);
 textControl1.Selection = selection;
 var selectedText = selection.Text;

To get the formatted text in any supported format, the Selection.Save method can be utilized. The following code shows how retrieve the word "Text" in HTML format.

string htmlString;

textControl1.Text = "TX Text Control";
TXTextControl.Selection selection = new TXTextControl.Selection(3, 4);
textControl1.Selection = selection;
selection.Save(out htmlString, TXTextControl.StringStreamType.HTMLFormat);

The variable htmlString contains the word "Text" in HTML format.

<?xml version="1.0" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta content="TX31_HTM 31.0.1101.500" name="GENERATOR" />
      <title></title>
   </head>
   <body style="font-family:'Arial';font-size:12pt;text-align:left;">
      <p lang="en-US" style="text-indent:0pt;margin-top:0pt;margin-bottom:0pt;font-size:10pt;">Text</p>
   </body>
</html>

Retrieving Text without Selecting Text

Using the above methods the text must be selected to retrieve the text. The TextChar class can be used to retrieve a character at a specific location without setting the input position to that location. The following code returns the character "T" at index 4.

var charAtInputPos = textControl1.TextChars[4].Char;

The Line class can be used to retrieve text from a specific line. The following code returns the complete text of the first line.

var lineText = textControl1.Lines[1].Text;

The third option to retrieve text without selecting text is the usage of the Paragraph class that represents a complete paragraph including text and formatting. The following code returns the plain text of the complete paragraph based on input position 3.

var parText = textControl1.Paragraphs.GetItem(3).Text;

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.InputPosition Class
  • TXTextControl.Selection Class
  • TXTextControl.TextChar Class

Related Posts

ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format

Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…


ActiveXASP.NETWindows Forms

Expert Implementation Services for Legacy System Modernization

We are happy to officially announce our partnership with Quality Bytes, a specialized integration company with extensive experience in modernizing legacy systems with TX Text Control technologies.


ActiveXASP.NETWindows Forms

Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5

TX Text Control 33.0 Service Pack 1 and TX Text Control 32.0 Service Pack 5 have been released, providing important updates and bug fixes across platforms. These service packs improve the…