Products Technologies Demo Docs Blog Support Company

How to Select the Currently Visible Text

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…

How to Select the Currently Visible Text

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.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

Windows FormsWPF.NET

Create a Table of Contents in Windows Forms using C#

This article explains how to create a table of contents in Windows Forms using the ribbon or programmatically. Creating a table of contents is required to organize large documents.


Windows FormsList.NET

Two Ways to Restart Numbered Lists in TX Text Control

In TX Text Control, numbered lists are continued by default and need to be reset when required. There is more than one way if you want to restart numbered lists in a document. In this article, two…


.NETSample

Paste Special: The Easy Way to Implement

In an older sample, I showed how to implement a paste special functionality by accessing the clipboard directly using .NET functionality. In version 15.0, we implemented this functionality…


.NETSampleSections

How to Remove All Section Breaks in a Document?

With version 14.0, we introduced document section breaks that allow you to have different page formats in the same document. Version 15.0 implements page columns that can be adjusted section-wise.…


.NETPrintingSample

Batch Printing: How to Print Documents in One Print Job

A typical requirement when printing loads of documents is managing the print jobs. A group of separate documents might be subsumed in a single print job. We just published a new sample in our…