Key events in datagrid control

C

Carl Tribble

Is there any way to reliably catch KeyUp events for Function keys in
particular while inside a datagrid?

I have a form with KeyPreview=True and I am using the Form.KeyUp event to
handle several function keys (F5, F6, etc.)

The problem is I have a datagrid on this form and it seems whenever the
datagrid has the focus, it consumes most of the key events without passing
them on to me. I have tried using a DataGridTextBox control, which helps
but it only gives some F-keys and only the KeyDown events (and occasioally
KeyPress) It does not provide any KeyUp events (except, for some reason,
the F10 key). For example, using the DataGridTextBox I can get the KeyDown
for F1 and F3 but not F2, and KeyUp does not occur for any of these keys.

Thanks,
-Carl
 
O

One Handed Man \( OHM - Terry Burns \)

Try this, untested . . .

Imports System.Windows.Forms
Public Class MyDataGrid
Inherits DataGrid

Public Sub New()
MyBase.New()

End Sub

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal
keyData As Keys) As Boolean
MyBase.ProcessCmdKey(msg, keyData)
Const WM_KEYDOWN As Int32 = &H100
Const WM_SYSKEYDOWN As Int32 = &H104

If msg.Msg = WM_KEYDOWN Or msg.Msg = WM_SYSKEYDOWN Then
Select Case keyData
Case Keys.Enter
Dim currentCell As DataGridCell = Me.CurrentCell
Dim rowNumber As Integer = currentCell.RowNumber
Dim rowCount As Integer = CType(Me.DataSource, DataTable).Rows.Count
Dim colNumber As Integer = currentCell.ColumnNumber

If colNumber <> 3 Then
Me.CurrentCell = New DataGridCell(rowNumber, 3)
Else
If rowNumber < rowCount - 1 Then
Me.CurrentCell = New DataGridCell(rowNumber + 1, 3)
Else
Me.CurrentCell = New DataGridCell(0, 3)
End If
End If
End Select
End If
End Function

End Class

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
C

Carl Tribble

Works great! Thank you very much, OHM.

-Carl


One Handed Man ( OHM - Terry Burns ) said:
Try this, untested . . .

Imports System.Windows.Forms
Public Class MyDataGrid
Inherits DataGrid

Public Sub New()
MyBase.New()

End Sub

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal
keyData As Keys) As Boolean
MyBase.ProcessCmdKey(msg, keyData)
Const WM_KEYDOWN As Int32 = &H100
Const WM_SYSKEYDOWN As Int32 = &H104

If msg.Msg = WM_KEYDOWN Or msg.Msg = WM_SYSKEYDOWN Then
Select Case keyData
Case Keys.Enter
Dim currentCell As DataGridCell = Me.CurrentCell
Dim rowNumber As Integer = currentCell.RowNumber
Dim rowCount As Integer = CType(Me.DataSource, DataTable).Rows.Count
Dim colNumber As Integer = currentCell.ColumnNumber

If colNumber <> 3 Then
Me.CurrentCell = New DataGridCell(rowNumber, 3)
Else
If rowNumber < rowCount - 1 Then
Me.CurrentCell = New DataGridCell(rowNumber + 1, 3)
Else
Me.CurrentCell = New DataGridCell(0, 3)
End If
End If
End Select
End If
End Function

End Class

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top