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
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes