Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Feb 24, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

View Selected Text From the Beginning

Most professional and commercial applications exhibit a specific behavior when using dialog boxes with text fields: When you tab to an input field, or hot-key (using some ALT-key combination), you fully select the existing text in the field. Any typing then replaces the entire field. However, simply clicking on the text field with the mouse just sets the focus without making any text selection.

The VB Knowledge Base documents how to set this behavior by capitalizing on the GetKeyState API call. However, the technique results in some inconvenience where the text itself is too large for the field width. You wind up looking at the tail end of the highlighted text, not the front end, so it can be difficult to tell what the current text contains.

By combining the GetKeyState API with use of SendKeys and the TextWidth method, you can define a complete subroutine solution where a tab or hot-key to a large text field selects the field, but leaves you looking at the text from the beginning, not the end.

First, declare the GetKeyState API function, and add the SelectWholeText routine, in your form:

Option Explicit

	' Recall that in a form, you need "Private" on an API.

	#If Win16 Then
		Private Declare Function GetKeyState Lib "User" _
			(ByVal iVirtKey As Integer) As Integer
		Private Declare Function GetKeyState Lib "User32" _
			(ByVal lVirtKey As Long) As Integer
	#End If

	' vbTab		' same as Chr$(&H9) - character constant
	' vbKeyTab	' same as decimal 9 - key code constant
	' vbKeyMenu	' same as decimal 18 (Alt key) - key code 
					' constant

Private Sub SelectWholeText(Ctl As Control)
	' If you got to the "Ctl" field via either TAB or an
	' Alt-Key, highlight the whole field. Otherwise select 
	' no text, since it must have received focus using a 
	' mouse-click.

	' Note difference between vbTab (character) and vbKeyTab 
	' (numeric constant). If vbTab were used, we'd have to 
	' Asc() it to get a number as an argument.

	' Use VB4/5's "With" to improve maintainability in case 
	' the parameter name changes.
	With Ctl
		If (GetKeyState(vbKeyTab) < 0) Or _
			(GetKeyState(vbKeyMenu) < 0) Then
			' We tabbed or used a hotkey - select all text. In
			' the case of a long field, use Sendkeys so we
			' see the beginning of the selected text.

			' TextWidth Method tells how much width a string 
			' takes up to display (default target object is 
			' the Form).
			If TextWidth(.Text) > .Width Then
				SendKeys "{End}", True
				SendKeys "+{Home}", True
				.SelStart = 0
				.SelLength = Len(.Text)
			End If
			.SelLength = 0
		End If
	End With
End Sub
Next, call the subroutine in the GotFocus event of any text field:
Private Sub txtPubID_GotFocus()
	SelectWholeText txtPubID
End Sub

Private Sub txtTitle_GotFocus()
	SelectWholeText txtTitle
End Sub
Mark Cohen
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date