Click here to Skip to main content
16,005,467 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: connecting access database Pin
Christian Graus18-Oct-06 4:32
protectorChristian Graus18-Oct-06 4:32 
QuestionDatagridview not refreshing Pin
Rey999918-Oct-06 3:07
Rey999918-Oct-06 3:07 
AnswerRe: Datagridview not refreshing Pin
Sebrell18-Oct-06 5:05
Sebrell18-Oct-06 5:05 
QuestionRe: Datagridview not refreshing Pin
Rey999918-Oct-06 5:28
Rey999918-Oct-06 5:28 
AnswerRe: Datagridview not refreshing [modified] Pin
Sebrell18-Oct-06 8:24
Sebrell18-Oct-06 8:24 
GeneralRe: Datagridview not refreshing Pin
Rey999918-Oct-06 20:40
Rey999918-Oct-06 20:40 
AnswerRe: Datagridview not refreshing Pin
Rey999919-Oct-06 4:34
Rey999919-Oct-06 4:34 
QuestionHow to trap keystrokes in .NET controls by using Visual Basic .NET Pin
hisuman10018-Oct-06 2:42
hisuman10018-Oct-06 2:42 
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

The system passes two parameters to the ProcessCmdKey method: msg and keyData. The msg parameter contains the Windows Message, such as WM_KEYDOWN. The keyData parameter contains the key code of the key that was pressed. If CTRL or ALT was also pressed, the keyData parameter contains the ModifierKey information.

Using the msg parameter is not mandatory; you can ignore it. It is good practice, however, to test the message. In this example, you test WM_KEYDOWN to verify that this is a keystroke event. You also test WM_SYSKEYDOWN, so that it is possible to catch keystroke combinations that involve control keys (primarily ALT and CTRL).

To trap specific keys, you can evaluate the keyCode by comparing it to the Keys enumeration. The following code sample demonstrates how to catch the keystrokes UP ARROW, DOWN ARROW, TAB, CTRL+M, and ALT+Z:
Const WM_KEYDOWN As Integer = &H100
Const WM_SYSKEYDOWN As Integer = &H104
If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
Select Case (keyData)
Case Keys.Down Console.WriteLine("Down Arrow Captured")
Case Keys.Up Console.WriteLine("Up Arrow Captured")
Case Keys.Tab Console.WriteLine("Tab Key Captured")
Case (Keys.Control Or Keys.M) Console.WriteLine("<ctrl> + m Captured")
Case (Keys.Alt Or Keys.Z) Console.WriteLine("<alt> + z Captured")
End Select
End If




Build an Example
The following example shows how to trap keystrokes with the DataGrid control.
1.Create a new Windows Control Library project in Visual Basic .NET or in Visual Basic 2005.
2.View the properties for the class UserControl1, and then change the name to MyDataGrid.
3.View the code for the Control Library, and then change the following line of code
Inherits System.Windows.Forms.UserControl
to the following:
Inherits System.Windows.Forms.DataGrid
4.Add the following method to the MyDataGrid class:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, _ ByVal keyData As Keys) As Boolean
Const WM_KEYDOWN As Integer = &H100
Const WM_SYSKEYDOWN As Integer = &H104
If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
Select Case (keyData)
Case Keys.Down Me.Parent.Text = "Down Arrow Captured"
Case Keys.Up Me.Parent.Text = "Up Arrow Captured"
Case Keys.Tab Me.Parent.Text = "Tab Key Captured"
Case (Keys.Control Or Keys.M) Me.Parent.Text = "<ctrl> + M Captured"
Case (Keys.Alt Or Keys.Z) Me.Parent.Text = "<alt> + Z Captured"
End Select
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
5.Build the project.
6.Create a new Windows Application project in Visual Basic .NET or in Visual Basic 2005. By default, Form1 is created.
Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.
For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnhcvs04/html/vs04k1.asp
For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/ms171843.aspx7. On the Tools menu, click Customize Toolbox.
8.Click the .NET Framework Components tab.
9.Click Browse, find the control/DLL that was just created, and then click OK.
10.The control MyDataGrid now appears in the toolbox. Place one on Form1.NOTE: You can use the code in the remaining steps to create sample data for the grid to display.
11.Add the following code to the namespace of the Form. You can place the code either before or after the Form Class definition.
' Structure is to provide sample data for the example.
Public Structure gridData
Private mmake As String
Private myear As Integer
Public Sub New(ByVal n As String, ByVal y As Integer)
mmake = n
myear = y
End Sub
Public Property Make() As String
Get
Return mmake
End Get
Set(ByVal Value As String)
Make = Value
End Set
End Property
Public Property Year()
As Integer
Get
Return myear
End Get
Set(ByVal Value As Integer)
myear = Value
End Set
End Property
End Structure
12.Add the following code to the form class immediately following the "Windows Form Designer generated code" section:
protected dataArray(5) As gridData

13.Add the following code to the Load event of Form1:
' Create some sample data.
dataArray(0) = New gridData("ford", 1999)
dataArray(1) = New gridData("chevrolet", 1999)
dataArray(2) = New gridData("plymouth", 1988)
dataArray(3) = New gridData("honda", 1999)
dataArray(4) = New gridData("fiat", 1987) ' Assign the data to the grid. MyDataGrid1.DataSource = dataArray

14.Run the sample, and try the various keystrokes that are being trapped (UP ARROW, DOWN ARROW, TAB, CTRL+M, and ALT+Z). The caption of the form is updated to show which keystroke was pressed.
AnswerRe: How to trap keystrokes in .NET controls by using Visual Basic .NET Pin
nlarson1118-Oct-06 3:35
nlarson1118-Oct-06 3:35 
GeneralRe: How to trap keystrokes in .NET controls by using Visual Basic .NET Pin
hisuman10018-Oct-06 19:00
hisuman10018-Oct-06 19:00 
QuestionScreen Scrape - I think? Pin
TommyACN18-Oct-06 2:17
TommyACN18-Oct-06 2:17 
AnswerRe: Screen Scrape - I think? Pin
Dave Kreskowiak18-Oct-06 3:36
mveDave Kreskowiak18-Oct-06 3:36 
QuestionCreating a simple datagrid Pin
dkoco18-Oct-06 2:06
dkoco18-Oct-06 2:06 
AnswerRe: Creating a simple datagrid Pin
nlarson1118-Oct-06 4:17
nlarson1118-Oct-06 4:17 
QuestionFinding subitems in Listview using LVM_FINDITEM Pin
Pradeepkumar79k18-Oct-06 1:19
Pradeepkumar79k18-Oct-06 1:19 
AnswerRe: Finding subitems in Listview using LVM_FINDITEM Pin
Dave Sexton18-Oct-06 2:44
Dave Sexton18-Oct-06 2:44 
QuestionUpdating a database using an adapter and datagrid Pin
Mark0618-Oct-06 0:57
Mark0618-Oct-06 0:57 
AnswerRe: Updating a database using an adapter and datagrid Pin
chirughosh18-Oct-06 1:07
chirughosh18-Oct-06 1:07 
GeneralRe: Updating a database using an adapter and datagrid Pin
Mark0618-Oct-06 2:04
Mark0618-Oct-06 2:04 
AnswerRe: Updating a database using an adapter and datagrid Pin
Mark0618-Oct-06 4:22
Mark0618-Oct-06 4:22 
QuestionVB.NET 2005 Startup object Pin
Ganesh India18-Oct-06 0:49
Ganesh India18-Oct-06 0:49 
AnswerRe: VB.NET 2005 Startup object Pin
Christian Graus18-Oct-06 1:00
protectorChristian Graus18-Oct-06 1:00 
GeneralRe: VB.NET 2005 Startup object Pin
Ganesh India18-Oct-06 1:07
Ganesh India18-Oct-06 1:07 
GeneralRe: VB.NET 2005 Startup object Pin
Christian Graus18-Oct-06 1:28
protectorChristian Graus18-Oct-06 1:28 
GeneralRe: VB.NET 2005 Startup object Pin
Ganesh India18-Oct-06 1:30
Ganesh India18-Oct-06 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.