December 5, 1995
This article explains how to allow the user of your Microsoft® Visual Basic® application use the enter key, instead of double-clicking with the mouse, to select a directory.
The Microsoft® Visual Basic® Directory List Box control displays a list of all directories stored on the specified disk drive. You can select a specific directory to work with by double-clicking its entry in the control. The selected directory can then be manipulated by your Visual Basic application.
You may, however, want to select a directory from the Directory List Box control by pressing the enter key instead of double-clicking with the mouse. This functionality can be accomplished by monitoring the KeyPress event of the Directory List Box control.
Whenever a key is pressed on the keyboard, a KeyPress event is triggered in the control that has the focus. A special number representing that particular key is stored in the KeyPress event's KeyAscii variable. You can then test the KeyAscii variable to determine whether a specific key, such as enter, was pressed on the keyboard.
In the example program below, each time a KeyPress event is triggered, the focus is set to the Directory List Box control. The KeyAscii variable is then tested to determine whether enter (represented by the number 13) was pressed. Next, a WM_LBUTTONDBLCLK (double-click) message is sent to the Directory List Box control by using the Microsoft Windows® application programming interface (API) SendMessage function. The KeyAscii variable is then set to a value of zero, which prevents the beep from being played on the computer's speaker. Finally, the default directory is changed to the newly selected directory.
This program shows how to emulate a double-click event in a Directory List Box control with the enter key.
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer,
ByVal lParam As Long) As Long
Const WM_LBUTTONDBLCLK = &H203
Private Sub Form_Load()
Text1.Text = ""
Text1.Text = CurDir$
End Sub
Private Sub Dir1_KeyPress(KeyAscii As Integer)
Dim R As Long
Dim DirHwnd As Integer
Dim X As String
If KeyAscii = 13 Then
Dir1.SetFocus
DirHwnd = GetFocus()
R = SendMessage(DirHwnd, WM_LBUTTONDBLCLK, 0, 0)
KeyAscii = 0
End If
X = Dir1.Path
ChDir X
Text1.Text = CurDir$
End Sub
Run the example program by pressing f5. The Directory List Box control displays a list of directories found on your hard drive. The name of the default directory appears in the Text Box control.
Select a directory from the Directory List Box control by pressing the first letter of the directory name or by clicking the directory name. Press enter. The currently selected directory is now the default directory.