You are currently offline, waiting for your internet to reconnect

Using UP ARROW and DOWN ARROW Keys to Move the Focus

This article was previously published under Q100413
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
SUMMARY
You can trap for the UP ARROW and DOWN ARROW extended keyboard keys in someVisual Basic controls by placing code in the KeyDown event procedure. Thecode uses KeyCode values to trap the UP ARROW and DOWN ARROW keys. Youcannot, however, trap the keys in all Visual Basic controls because somecontrols already have built-in functionality for the UP ARROW and DOWNARROW keys, so there is no KeyDown event generated.
MORE INFORMATION
The information in this article is provided to show that it is possible totrap the UP ARROW and DOWN ARROW keys, however Microsoft does not recommendthat you implement it because the UP ARROW and DOWN ARROW keys havestandard, predefined behavior on some controls. Microsoft recommends thatyou use the standard method for using the keyboard to move the focus; thatis, use the TAB and SHIFT+TAB keys or use the access keys.

Step-by-Step Example for Moving the Focus Using UP ARROW and DOWN ARROW

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running.
  2. Add a Picture box and two Text boxes to Form1.
  3. In the Picture1_KeyDown event procedure, add this code:
       Sub Picture1_KeyDown(KeyCode AS INTEGER, Shift AS INTEGER)      IF KeyCode = 38 Then    '* 38 = up arrow key         Text2.SetFocus      Text2.SelStart = 0   '* set the cursor to the start      END IF      IF KeyCode = 40 Then    '* 40 = down arrow key         Text1.SetFocus      Text1.SelStart = 0   '* set the cursor to the start      END IF   END SUB						
  4. In the Text1_KeyDown event procedure, add this code:
       Sub Text1_KeyDown(KeyCode AS INTEGER, Shift AS INTEGER)      If KeyCode = 38 Then    '* 38 = UP ARROW key         Picture1.SetFocus      End If      If KeyCode = 40 Then    '* 40 = DOWN ARROW key         Text2.SetFocus         Text2.SelStart = 0   '* set the cursor to the start      End If   End Sub						
  5. In the Text2_KeyDown event procedure, add this code:
       Sub Text2_KeyDown(KeyCode AS INTEGER, Shift AS INTEGER)      If KeyCode = 38 Then    '* 38 = UP ARROW key         Text1.SetFocus         Text1.SelStart = 0   '* set the cursor to the start      End If      If KeyCode = 40 Then    '* 40 = DOWN ARROW key         Picture1.SetFocus      End If   End Sub						
  6. Choose Start from the Run menu or press F5 to run the example. Press the UP ARROW or DOWN ARROW key to see the focus move to a different control.
If you use the LEFT ARROW or RIGHT ARROW keys, you can scroll in aText box, but these keys are ignored in the Picture box in this example.
2.00 3.00 B_VBmsdos
Properties

Article ID: 100413 - Last Review: 08/16/2005 21:27:25 - Revision: 2.1

  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 2.0 Professional Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 1.0 Standard Edition
  • Microsoft Visual Basic for MS-DOS
  • KB100413
Feedback