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

| Author: | TX Text Control Support Department |
| Language: | Visual Basic |
| Version: | 1.1 |
| Released: | March 20, 2001 |
| Last modified: | January 11, 2008 |
| Requirements: | TX Text Control ActiveX with Visual Basic |
| Download code: | scrolling_part2.zip |
This week we will continue with our article about scrolling in a search context. Last week's edition is archieved here.
What we have now is usable, but we can still improve upon it.
Currently, when we are stepping through the found results we use TX Text Control's built-in search feature to highlight the found word. But if we do the scrolling manually, we can move the highlight right to the top line on the screen; thus the user can easily see what has been highlighted.
Here is how it works. We have simply removed the HideSelection calls from the Load and Unload handlers so TX Text Control does not interfere with our custom scrolling. Then after the selection has been set in the Button handlers, the Sub MovePosToFirstLine is called given the input position of the selection as a parameter.
Here is the VB code:
Private Sub MovePosToFirstLine(nPos As Long) Dim lResult As Long, lr As LineRect ' Get the absolute line number of the position lResult = SendMessage2(Form1.TXTextControl1.hwnd, TX_LINEFROMCHAR, 0, nPos) If lResult = 0 Then Exit Sub End If lr.lLineIndex = lResult - 1 ' Now query the offset of this line lResult = SendMessage(Form1.TXTextControl1.hwnd, TX_GETLINERECT, 0, lr) Form1.TXTextControl1.ScrollPosY = lResult End Sub
First of all, the absolute line that contains the given position is determined. This is done with the TX_LINEFROMCHAR message. If no error occurs, the result is assigned to the lLineIndex member of the LineRect structure. This structure is sent to TX Text Control via the TX_GETLINERECT message which returns the offset of the line in twips to the very top of the page. This value is the value needed for the ScrollPosY method to scroll right to the found line.
The only tricky thing is the use of the SendMessage API call, but as a Windows user, you should at least have heard of it. In the next version of TX Text Control it will not be necessary to use SendMessage here, because TX 9.0 will come up with a bunch of new methods.
This tip can be used in other cases, as well. For example, if you want to be able to scroll to a given chapter in your application and you have marked the beginning of the chapter with a field, then you scroll right to that chapter by supplying the FieldStart value to the MovePosToFirstLine sub.