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:
IN THIS TASKSUMMARY 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 topDevelop the Custom Windows Forms Control- Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- Create a new Class Library project by using Visual Basic .NET or Visual Basic 2005.
- Name the project MyCustomControl. By
default, Class1.vb is created.
- In Solution Explorer, right-click References, and then click Add
References.
- 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. - Click OK to close the Add References dialog
box.
- Rename the Class1.vb
MyTextBox.vb.
- 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
- On the Build menu, click Build
Solution.
back to the topFire the KeyDown Event and the KeyUp Event- Start Visual Studio .NET or Visual Studio 2005.
- Create a new Windows Application project by using Visual Basic .NET or Visual Basic 2005.
- Name the project KeyUpDownTest. By
default, Form1.vb is created.
- 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. - In Customize Toolbox dialog box, click the
.NET Framework Components tab.
- Click Browse, and then locate
MyCustomControl.dll (typically in the \bin folder of the
MyCustomControl application folder).
- Click OK to close the Customize
Toolbox dialog box.
- Drag MyTextBox from the toolbox (under
Windows Forms) to Form1.vb. By default,
the MyTextBox1 control is created in Form1.
- Right-click Form1.vb, and then click View Code.
- 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 - 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 - On the Debug menu, click
Start.
- Press TAB in the
MyTextBox1
control. Notice that the KeyDown event and the KeyUp event are fired.
back to the topREFERENCES For more information, visit the following Microsoft Web
sites: back to the top
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005applies kbvs2005swept kbEvent kbControl kbWindowsForms kbHOWTOmaster KB327821 kbAudDeveloper |
---|
|