How to fire KeyDown or KeyUp while pressing TAB in a Windows Forms control by using Visual Basic .NET or Visual Basic 2005 (327821)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q327821

For a Microsoft Visual C# .NET version of this article, see 327823.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System.Windows.Forms

IN THIS TASK

SUMMARY

This step-by-step article describes how to fire the KeyDown event or the KeyUp event when you press TAB in a Windows Forms control. By default, the KeyDown event and the KeyUp event do not fire for a control when you press TAB. However, the KeyUp event is fired for the next control that receives focus. To fire the KeyDown event or the KeyUp event when you press TAB, create a custom Windows Forms control, and then override the IsInputKey method. IsInputKey determines whether the specified key is a regular input key or a special key that requires preprocessing.

Note When you trap the TAB key in a control KeyDown or KeyUp event, the TAB key does not function as a navigation key for the corresponding control.

back to the top

Develop the Custom Windows Forms Control

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. Create a new Class Library project by using Visual Basic .NET or Visual Basic 2005.
  3. Name the project MyCustomControl. By default, Class1.vb is created.
  4. In Solution Explorer, right-click References, and then click Add References.
  5. On the .NET tab, locate System.Windows.Forms.dll, and then click Select.

    Note In Visual Studio 2005, you do not have to click Select.
  6. Click OK to close the Add References dialog box.
  7. Rename the Class1.vb MyTextBox.vb.
  8. Replace the existing code in MyTextBox.vb with the following code:
    Imports System.Windows.Forms
    
    Public Class MyTextBox
     Inherits System.Windows.Forms.TextBox
     ' Override the IsInputKey method to identify the special keys.
     Protected Overrides Function IsInputKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
      Select Case keyData
       ' Add the list of special keys that you want to handle.
      Case Keys.Tab
        Return True
       Case Else
        Return MyBase.IsInputKey(keyData)
      End Select
     End Function
    End Class
    
  9. On the Build menu, click Build Solution.
back to the top

Fire the KeyDown Event and the KeyUp Event

  1. Start Visual Studio .NET or Visual Studio 2005.
  2. Create a new Windows Application project by using Visual Basic .NET or Visual Basic 2005.
  3. Name the project KeyUpDownTest. By default, Form1.vb is created.
  4. In the toolbox, right-click Windows Forms, and then click Customize Toolbox.

    Note In the toolbox, right-click Windows Forms, and then click Choose Items.
  5. In Customize Toolbox dialog box, click the .NET Framework Components tab.
  6. Click Browse, and then locate MyCustomControl.dll (typically in the \bin folder of the MyCustomControl application folder).
  7. Click OK to close the Customize Toolbox dialog box.
  8. Drag MyTextBox from the toolbox (under Windows Forms) to Form1.vb. By default, the MyTextBox1 control is created in Form1.
  9. Right-click Form1.vb, and then click View Code.
  10. Add the following code to handle the KeyDown event for the MyTextBox1 control:
    Private Sub MyTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyTextBox1.KeyDown
     Debug.WriteLine("KeyDown :" + e.KeyCode.ToString())
    End Sub
  11. Add the following code to handle the KeyUp event for the MyTextBox1 control:
    Private Sub MyTextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyTextBox1.KeyUp
     Debug.WriteLine("KeyUp :" + e.KeyCode.ToString())
    End Sub
  12. On the Debug menu, click Start.
  13. Press TAB in the MyTextBox1 control. Notice that the KeyDown event and the KeyUp event are fired.
back to the top

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbEvent kbControl kbWindowsForms kbHOWTOmaster KB327821 kbAudDeveloper