| Skype: | TextControlSupport | |
| Orders: | 877-462-4772 |

| 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 |
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:
Private Sub Search() Dim pos As Integer Dim options As Integer Dim n As Integer options = 24 ' Suppress message box and do not highlight found text If bCase Then options = options + 4 End If nResults = 0 pos = Form1.TXTextControl1.Find(szWhat, 0, options) While pos <> -1 nResults = nResults + 1 pos = Form1.TXTextControl1.Find(szWhat, pos + 1, options) Wend If (nResults > 0) Then ReDim nPositions(nResults) pos = Form1.TXTextControl1.Find(szWhat, 0, options) n = 0 While pos <> -1 nPositions(n) = pos n = n + 1 pos = Form1.TXTextControl1.Find(szWhat, pos + 1, options) Wend End If Label2.Caption = "Found " & Str(nResults) & " matches" nCurrent = -1 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.