|
|
Tracking A Textbox Cursor This article provides a brief description of how to track a textbox's cursor position and selection data.
[edit] Reasons for TrackingWe might like to know the current position of the cursor and it's selection state within a textbox to provide a means to add or replace some standardized text into the string at the given location. For instance, in a case where we'd like to have the user be able to click a button to add a keyword of some sort into the text at the current position of the cursor.
[edit] Problems in TrackingIn the scenario described above, when the user clicks a button, or otherwise causes the loss of focus on the textbox, the SelStart and SelLength properties are no longer programmatically available to us. Therefore, we require a method to store this information for use when the user has the focus set somewhere other than the textbox in question.
[edit] How to Track the CursorTracking the cursor data is a simple matter of using two events of the textbox (the KeyUp and MouseUp events) and providing two form scope variables to hold the information that other code in the form will be able to access. Here is an example of how it works: CODE Option Compare Database Option Explicit 'start position of the cursor within ctlText Private CursorPosition As Integer 'selection length of the cursor within ctlText Private CursorLen As Integer 'track the cursor state when a key is released Private Sub ctlText_KeyUp(KeyCode As Integer, Shift As Integer) CursorPosition = Me.ctlText.selStart CursorLen = Me.ctlText.SelLength End Sub 'track the cursor state when the mouse button is released Private Sub ctlText_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) CursorPosition = Me.ctlText.selStart CursorLen = Me.ctlText.SelLength End Sub
[edit] See AlsoFunction: ReplaceViaPosition.
|
| This page was last modified 02:05, 9 February 2012. This page has been accessed 363 times. Disclaimers |