Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
What is this?Syndicate this content

Scrolling in TX Text Control - Part I

This source code snippet requires TX Text Control ActiveX
Author:TX Text Control Support Department
Language:Visual Basic
Version:1.1
Released:April 24, 2001
Last modified:January 11, 2008
Requirements:TX Text Control ActiveX with Visual Basic
Download code:scrolling_part1.zip Download [5 KB, ZIP]

In this week's Top TX Tip column, we are dealing with scrolling. TX Text Control does most of the work for you, but we still get quite a few support queries about the subject.

First of all, take a look at the size archive below. Then you will see that we wrote a short sample that simply searches a text for a given word/phrase. This is not of much use you will say, because TX already has a built-in search dialog. This is of course true, but the sample is only meant to show how to scroll to a certain position in the document.

The new find dialog searches the entire document in one step and saves the found positions in an array. The end user can then click from one found position to the next and back (just like one would expect from a search dialog).

Here is the VB code for the search sub:

  1. Private Sub Search()
  2.  
  3. Dim pos As Integer
  4. Dim options As Integer
  5. Dim n As Integer
  6.  
  7. options = 24 ' Suppress message box and do not highlight found text
  8.  
  9. If bCase Then
  10. options = options + 4
  11. End If
  12.  
  13. nResults = 0
  14.  
  15. pos = Form1.TXTextControl1.Find(szWhat, 0, options)
  16.  
  17. While pos <> -1
  18. nResults = nResults + 1
  19. pos = Form1.TXTextControl1.Find(szWhat, pos + 1, options)
  20. Wend
  21.  
  22. If (nResults > 0) Then
  23.  
  24. ReDim nPositions(nResults)
  25.  
  26. pos = Form1.TXTextControl1.Find(szWhat, 0, options)
  27. n = 0
  28.  
  29. While pos <> -1
  30. nPositions(n) = pos
  31. n = n + 1
  32. pos = Form1.TXTextControl1.Find(szWhat, pos + 1, options)
  33. Wend
  34. End If
  35.  
  36. Label2.Caption = "Found " & Str(nResults) & " matches"
  37.  
  38. nCurrent = -1
  39.  
  40. End Sub

This code is not very efficient, because it searches the document twice (once it know how many matches are found and once to store the positions), but it's just for demonstration... We are sure that you can think up a hundred ways to make the code more efficient :-).

nPosition is a global variable that stores the positions and nCurrent is a variable that holds the number of the currently displayed result.

Now, all we have to do is make TX Text Control scroll - that is pretty easy. When the "Next match" button is pressed, the selection is set to the stored position and the word is highlighted. And now comes the trick. TX will scroll the selection automatically into the visible area, but only if the HideSelection property is set to False (by default it's True). If this is not done, TX will not scroll, because it doesn't have the focus.

But how do I get a certain line to appear at the top of the scroll window? This is a lot harder to do and so we will cover that next week. It takes some math and some TX message calls to be accomplished. So, if you are interested, be sure to get the next newsletter.

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place