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:
  • 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 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

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. Create a new Class Library project by using Visual C# .NET or Visual C# 2005.
  3. Name the project MyCustomControl. By default, Class1.cs is created.
  4. In Solution Explorer, right-click References, and then click Add References.
  5. On the .NET tab, locate System.Windows.Forms.dll, click Select, and then click OK.
  6. Rename Class1.cs as MyTextBox.cs.
  7. 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);
    		}
    	}
    }
  8. On the Build menu, click Build Solution.
back to the top

Fire the KeyDown Event and the KeyUp Event


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

back to the top

REFERENCES

For more information, see the following Microsoft Developer Network (MSDN) Web sites:




back to the top


Modification Type:MajorLast Reviewed:1/18/2006
Keywords:kbEvent kbControl kbWindowsForms kbHOWTOmaster KB327823 kbAudDeveloper