How to trap keystrokes in .NET controls by using Visual Basic .NET
-
I have down loaded the following code form Microsoft site and it is working. But I am unable to understand how I will trap it in my keyup or keypress event or any other way. Can any body help me please. With regards Suman How to trap keystrokes in .NET controls by using Visual Basic .NET or Visual Basic 2005 View products that this article applies to. On This Page SUMMARY Set Up the Key Trap Implement the Overridden Method Build an Example SUMMARY This step-by-step article demonstrates how to trap keystrokes in Windows Forms controls. By using the sample code in this article, you can intercept almost any individual keystroke. You also can intercept key combinations, including CTRL and ALT. The Print Screen key is not affected by this technique. Additionally, some keystrokes from keyboards with additional keys, such as keys that control a Web browser or a CD-ROM player, might not be captured. For most purposes, the standard KeyUp, KeyDown, and KeyPress events are enough to capture and handle keystrokes. However, not all controls raise these events for all keystrokes under all conditions. For example, consider the DataGrid control: If no data has been assigned to the grid, the arrow keys (LEFT ARROW, RIGHT ARROW, UP ARROW, and DOWN ARROW) raise only the KeyUp event. Other keys, such as A or 4, raise all three events. If the DataGrid is currently displaying data, none of the standard keyboard events are raised for the navigation keys. Keystrokes such as A or 4 raise no events, raise only KeyUp, or raise all three events, depending on what is currently selected in the control. In these situations, you can follow the steps in this article to capture keystrokes, regardless of the state of the control. The code samples in this article are written to work with the DataGrid, because this is the control for which this feature is most frequently requested. You can use this same approach with other .NET controls. Set Up the Key Trap To trap keystrokes in a Windows Forms control, you derive a new class that is based on the class of the control that you want. You override the ProcessCmdKey method. In this overridden method, you will place the code to process the keystrokes that you want to trap. The following sample code is an example of the basic structure for such a class: Class MyDataGrid Inherits DataGrid Protected Overrides Function ProcessCmdKey( ByRef msg As Message, ByVal keyData As Keys ) As Boolean End Function End Class Implement the Overridden Method T
-
I have down loaded the following code form Microsoft site and it is working. But I am unable to understand how I will trap it in my keyup or keypress event or any other way. Can any body help me please. With regards Suman How to trap keystrokes in .NET controls by using Visual Basic .NET or Visual Basic 2005 View products that this article applies to. On This Page SUMMARY Set Up the Key Trap Implement the Overridden Method Build an Example SUMMARY This step-by-step article demonstrates how to trap keystrokes in Windows Forms controls. By using the sample code in this article, you can intercept almost any individual keystroke. You also can intercept key combinations, including CTRL and ALT. The Print Screen key is not affected by this technique. Additionally, some keystrokes from keyboards with additional keys, such as keys that control a Web browser or a CD-ROM player, might not be captured. For most purposes, the standard KeyUp, KeyDown, and KeyPress events are enough to capture and handle keystrokes. However, not all controls raise these events for all keystrokes under all conditions. For example, consider the DataGrid control: If no data has been assigned to the grid, the arrow keys (LEFT ARROW, RIGHT ARROW, UP ARROW, and DOWN ARROW) raise only the KeyUp event. Other keys, such as A or 4, raise all three events. If the DataGrid is currently displaying data, none of the standard keyboard events are raised for the navigation keys. Keystrokes such as A or 4 raise no events, raise only KeyUp, or raise all three events, depending on what is currently selected in the control. In these situations, you can follow the steps in this article to capture keystrokes, regardless of the state of the control. The code samples in this article are written to work with the DataGrid, because this is the control for which this feature is most frequently requested. You can use this same approach with other .NET controls. Set Up the Key Trap To trap keystrokes in a Windows Forms control, you derive a new class that is based on the class of the control that you want. You override the ProcessCmdKey method. In this overridden method, you will place the code to process the keystrokes that you want to trap. The following sample code is an example of the basic structure for such a class: Class MyDataGrid Inherits DataGrid Protected Overrides Function ProcessCmdKey( ByRef msg As Message, ByVal keyData As Keys ) As Boolean End Function End Class Implement the Overridden Method T
-
Rey, Have you tried to do something simple like turning keypreview property of the form on? Inside the form's keypress/keyup events use activecontrol to know what control is receiving the keystrokes. It's always worked for me in the past... nathan
Thnak you Mr. Nathan,It is working. With Regards Suman