Select the content of TextBox and ComboBox controls

(Visual Basic 6)

One of the features of the vast majority of TextBox and ComboBox controls in Windows is that the text in the control becomes selected when the control received the focus. This can be a great help to the user, who does not then need to select the text in the control in order to replace it: the new data can just be typed in as if the control were previously empty.

There is a slight addition to be made to this rule, however. When the focus is being set to a TextBox, the content does not become selected if the focus was set using the mouse (i.e., by clicking on the control). This is an important distinction, as often when the focus is set using the mouse it is because the user is trying to select a range of text within the control (to copy or cut it, for example).

Unfortunately, TextBox and ComboBox controls in VB do not automatically support this behaviour (though I can't say I understand why not!). It is easy (though tedious) to add this functionality to a project by inserting code in the GotFocus event of every single TextBox and ComboBox control. Ideally this code is kept as short as possible for fairly obvious reasons.

This code sample contains a procedure that achieves all of the effects described above, and which can be called very simply from within your project. After adding the procedure, just a single line of code (and it's the same every time -- there's no need to tailor it for each control) can be placed in the GotFocus event of each TextBox and ComboBox control. This behaviour always adds a professional feel to any application.

Option Explicit 'API declarations for SelectControlContent procedure Private Declare Function GetAsyncKeyState Lib "USER32" (ByVal vKey As Long) As Integer Private Const VK_LBUTTON = &H1 Public Sub SelectControlContent() Dim lKeyState As Long 'return value from the GetAsynclKeyState API call Dim bMouseDown As Boolean 'flag to store whether the left mouse button is currently pressed 'First detect whether the mouse-button is currently pressed. 'If it is, the user must have clicked into this field, so we won't highlight the selection lKeyState = GetAsyncKeyState(VK_LBUTTON) 'Check bit15 of the return value; if it is selected, the mouse is pressed If (lKeyState And &H8000) Then bMouseDown = True 'Make sure spurious calls are ignored If Not Screen.ActiveControl Is Nothing Then With Screen.ActiveControl 'Is this a TextBox? If TypeOf Screen.ActiveControl Is TextBox Then 'Is the left mouse pressed? If Not bMouseDown Then 'No -- so the user must have Tabbed to this field, used an accelerator, or the 'system used a .SetFocus (or similar) method to activate the field 'Select the contents of the TextBox .SelStart = 0 .SelLength = Len(.Text) End If 'Is this a ComboBox? ElseIf TypeOf Screen.ActiveControl Is ComboBox Then 'Can the user type into this ComboBox? If .Style = vbComboDropdown Or .Style = vbComboSimple Then 'Select the contents of the ComboBox .SelStart = 0 .SelLength = Len(.Text) End If End If End With End If End Sub

To use the procedure, just place the following code in the GotFocus event of each TextBox and ComboBox control in your project:

SelectControlContent

...and that is all that is needed! One thing to be careful of: ensure that you don't accidentally place the call to SelectControlContent in the Change event of your controls.

If you have any comments or suggestions regarding this article, please don't hesitate to contact me.

This article is copyright © Adam Dawes, 2000.