View Selected Text From the Beginning

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	#Else		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 					' constantPrivate 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			Else				.SelStart = 0				.SelLength = Len(.Text)			End If		Else			.SelLength = 0		End If	End WithEnd Sub

Next, call the subroutine in the GotFocus event of any text field:

 Private Sub txtPubID_GotFocus()	SelectWholeText txtPubIDEnd SubPrivate Sub txtTitle_GotFocus()	SelectWholeText txtTitleEnd Sub
See also  How to Avoid Money Transfer Scams  

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist