Microsoft KB Archive/327823

= How to fire the KeyDown event or the KeyUp event while pressing TAB in Windows Forms control by using Visual C# .NET or Visual C# 2005 =

Article ID: 327823

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-



This article was previously published under Q327823





For a Microsoft Visual Basic .NET version of this article, see 327821.

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

IN THIS TASK

 * SUMMARY


 * Develop the Custom Windows Forms 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 the TAB key in a Windows Forms control. By default, the KeyDown event and the KeyUp event do not fire for a control when you press the TAB key. 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 the TAB key, create a Custom Windows Form control, and override the IsInputKey method. The IsInputKey method determines whether the specified key is a regular input key, or a special key that requires pre-processing.

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 C# .NET or Visual C# 2005. Name the project MyCustomControl. By default, Class1.cs is created. In Solution Explorer, right-click References, and then click Add References. On the .NET tab, locate System.Windows.Forms.dll, click Select, and then click OK. Rename Class1.cs as MyTextBox.cs .</li>  Replace the code in MyTextBox.cs with the following code: using System; using System.Windows.Forms; public class MyTextBox :System.Windows.Forms.TextBox {   // Override IsInputKey method to identify the Special keys protected override bool IsInputKey( System.Windows.Forms.Keys keyData ) {       switch ( keyData) {           // Add the list of special keys that you want to handle case Keys.Tab: return true; default: return base.IsInputKey(keyData); }   } } </li> On the Build menu, click Build Solution.</li></ol>

back to the top

Fire the KeyDown Event and the KeyUp Event
<ol> Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.</li> Create a new Windows Application project by using Visual C# .NET or Visual C# 2005.</li> Name the project KeyUpDownTest. By default, Form1.cs is created.</li> On the toolbox, right-click Windows Forms, and then click Customize Toolbox.</li> In the Customize Toolbox dialog box, click the .NET Framework Components tab.</li> Click Browse, locate MyCustomControl.dll (typically in the \bin\debug folder of the MyCustomControl application folder), and then click OK.</li> Drag MyTextBox from the toolbox (under Windows Forms) to Form1.cs. By default, the myTextBox1 control is created in Form1.</li> Right-click Form1.cs, and then click View Code.</li>  Add the following code to the using section: using System.Diagnostics; </li> In Solution Explorer, double-click Form1.cs, click to select myTextBox1, click Events, and then double-click KeyDown.</li>  Add the following code to the myTextBox1_KeyDown event: Debug.WriteLine(&quot; KeyDown : &quot; + e.KeyCode.ToString); </li> Right-click Form1.cs, click View Designer, click Events, and then double-click KeyUp.</li>  Add the following code to the myTextBox1_KeyUp event: Debug.WriteLine(&quot; KeyUp : &quot; + e.KeyCode.ToString); </li> On Debug menu, click Start.</li> <li>Press the TAB key in myTextBox1 control. Notice that the KeyDown and KeyUp events are fired.</li></ol>

back to the top

<div class="references_section">