dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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

 

 

Sitemap
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date