Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
« »

How to select the currently visible text

Blogged by Björn Meyer on January 29, 2007 and tagged with Samples, .NET.

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.

« Prev:Server word processing: Choosing the right class
» Next:Printing without the status dialog

Comments

If you would like to comment on this blog entry, please post a comment.

Posted by Pattanaik on Tuesday, February 13, 2007 at 07:58:49:

Can I a insert a set of words to a word document to a perticualr position?

Posted by Björn Meyer on Tuesday, February 13, 2007 at 14:20:53:

Hello
Yes, this is possible. Using TX Text Control, you can load an MS Word document and insert text using the Selection.Text method at a specific position.
External verification page for ISO 9000:2000 certificate
ISO 9001:2000
certified