Products Technologies Demo Docs Blog Support Company

How to Select the Currently Visible Text

Processing entire documents for syntax highlighting becomes slow with thousands of pages. Combining the InputPosition and ScrollLocation classes in TX Text Control allows developers to identify visible text boundaries and restrict processing to only the on-screen portion.

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

TX Text Control version 15.0 introduced a ClipboardFormat parameter on the Paste method, enabling native Paste Special functionality. The GetClipboardFormats method returns all available clipboard…


.NETSampleSections

How to Remove All Section Breaks in a Document?

TX Text Control 15.0 adds per-section page column support alongside existing section breaks. To remove all section breaks programmatically, iterate through SectionCollection using…


.NETPrintingSample

Batch Printing: How to Print Documents in One Print Job

Batch printing multiple documents as a single print job using TX Text Control relies on a .NET PrintDocument with PrintPage and QueryPageSettings events. Each page is rendered individually via the…

Share on this blog post on: