By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,515 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,515 IT Pros & Developers. It's quick & easy.

Key events in datagrid control

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"Carl Tribble" <ca*********@sbcglobal.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #2

P: n/a
Works great! Thank you very much, OHM.

-Carl
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

"Carl Tribble" <ca*********@sbcglobal.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.