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 (327823)
The information in this article applies to:
- Microsoft Visual C# .NET (2002)
- 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:
IN THIS TASKSUMMARY 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
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 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.
- 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);
}
}
} - On the Build menu, click Build
Solution.
back to the
topFire the KeyDown Event and the KeyUp Event- Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- Create a new Windows Application project by using Visual C#
.NET or Visual C# 2005.
- Name the project KeyUpDownTest. By
default, Form1.cs is created.
- On the toolbox, right-click Windows Forms,
and then click Customize Toolbox.
- In the Customize Toolbox dialog box, click
the .NET Framework Components tab.
- Click Browse, locate
MyCustomControl.dll (typically in the \bin\debug folder of the
MyCustomControl application folder), and then click
OK.
- Drag MyTextBox from the toolbox (under
Windows Forms) to Form1.cs. By default, the myTextBox1 control is created in Form1.
- Right-click Form1.cs, and then click View Code.
- Add the following code to the using section:
using System.Diagnostics; - In Solution Explorer, double-click
Form1.cs, click to select myTextBox1, click
Events, and then double-click
KeyDown.
- Add the following code to the myTextBox1_KeyDown event:
Debug.WriteLine(" KeyDown : " + e.KeyCode.ToString()); - Right-click Form1.cs, click View
Designer, click Events, and then double-click
KeyUp.
- Add the following code to the myTextBox1_KeyUp event:
Debug.WriteLine(" KeyUp : " + e.KeyCode.ToString()); - On Debug menu, click
Start.
- Press the TAB key in
myTextBox1 control. Notice that the KeyDown and KeyUp events are fired.
back to the
topREFERENCES For more information, see the following Microsoft Developer
Network (MSDN) Web sites: back to the
top
Modification Type: | Major | Last Reviewed: | 1/18/2006 |
---|
Keywords: | kbEvent kbControl kbWindowsForms kbHOWTOmaster KB327823 kbAudDeveloper |
---|
|