When working with very large documents with hundreds, maybe thousands of pages and using the Selection class to manipulate the text based on the user's interaction, you need to take some care with performance.

Sample: Syntax highlighting.

You are trapping the keystrokes for spaces, returns or other control characters to find out whether the user is typing a new word. If s/he is, you need to match the currently written word with a list of predefined words. If the word is found, the color is changed.

As long as the user is typing, this approach works fine. However, should the user loads a specific document and chooses to switch the syntax highlighting, you need to parse the whole text in order to highlight the words. TX Text Control's Selection class is very fast, but if you have thousands of pages, it might take some time.

The solution for this problem is to consider only the visible text. This is also very interesting when the user uses the scrollbars to scroll to a new position in the text. You just need to parse the visible text and to highlight the visible words.

TX Text Control offers a very powerful class to set the input position to a specific position in the document: The InputPosition class.

This class represents the current text input position of a document and can be instantiated with the return value of the ScrollLocation class that returns the current scroll position of the document.

textControl1.InputPosition = new TXTextControl.InputPosition(new Point(0,textControl1.ScrollLocation.Y));

This combination can be used to get the start and the end value for a new selection:

private void selectVisibleText()
{
  int startPosition = 0;
  int endPosition = 0;
  textControl1.InputPosition = new TXTextControl.InputPosition(new Point(0,textControl1.ScrollLocation.Y));
  startPosition = textControl1.InputPosition.TextPosition;
  textControl1.InputPosition = new TXTextControl.InputPosition(new Point(0, textControl1.ScrollLocation.Y + (textControl1.Height * (int)DpiX)));
  endPosition = textControl1.InputPosition.TextPosition;
  textControl1.Select(startPosition, endPosition - startPosition);
  textControl1.Focus();
}

DpiX is the 'Twips per pixel' value to convert the height of the control to a Twips value that is required by the InputPosition class.