Microsoft KB Archive/327821

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

Article ID: 327821

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



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
 * Develop the Custom Windows Form Control
 * Fire the KeyDown Event and the KeyUp Event
 * REFERENCES



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
 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.</li> Rename the Class1.vb MyTextBox.vb .</li>  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 </li> On the Build menu, click Build Solution.</li></ol>

back to the top

Fire the KeyDown Event and the KeyUp Event
<ol> Start Visual Studio .NET or Visual Studio 2005.</li> Create a new Windows Application project by using Visual Basic .NET or Visual Basic 2005.</li> Name the project KeyUpDownTest. By default, Form1.vb is created.</li> 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.</li> In Customize Toolbox dialog box, click the .NET Framework Components tab.</li> Click Browse, and then locate MyCustomControl.dll (typically in the \bin folder of the MyCustomControl application folder).</li> Click OK to close the Customize Toolbox dialog box.</li> Drag MyTextBox from the toolbox (under Windows Forms) to Form1.vb. By default, the MyTextBox1 control is created in Form1.</li> Right-click Form1.vb, and then click View Code.</li>  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(&quot;KeyDown :&quot; + e.KeyCode.ToString) End Sub </li>  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(&quot;KeyUp :&quot; + e.KeyCode.ToString) End Sub </li> On the Debug menu, click Start.</li> Press TAB in the MyTextBox1 control. Notice that the KeyDown event and the KeyUp event are fired.</li></ol>

back to the top

<div class="references_section">