Sanmarie
asked on
Strange behaviour with ProcessKeyPreview and ProcessCmdKey
Using .NET windows Forms, .NET 1.1 version
I have a custom datagrid, inherited from a windows form datagrid. The grid has rows and columns. I prevent the user from using the arrow keys (up, down, left, right) to go to certain rows or columns. I used the ProcessKeyPreview function of the datagrid to control the arrow keys in another project. In this project, I have more or less the same exact custom datagrid and the ProcessKeyPreview doesn't get fired at all. The strange thing is the ProcessCmdKey is what gets fired. Anyone experienced this? Below is the code. The same exact definition is in ProcessKeyPreview and ProcessCmdKey
**************ProcessKeyPr eview
Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Messa ge) As Boolean
If Not _dt Is Nothing Then
GetKeyStatus()
If (m.Msg = WM_KEYDOWN Or (m.Msg = WM_SYSKEYDOWN)) Then
'ignore the key if invalid edit entered
Select Case m.WParam.ToInt32
Case Keys.Up
If _invalidUpKey = True Then
Return True
Else
Return MyBase.ProcessKeyPreview(m )
End If
Case Keys.Down
If _invalidDownKey = True Then
Return True
Else
Return MyBase.ProcessKeyPreview(m )
End If
Case Keys.Left
If _invalidLeftKey = True Then
Return True
Else
Return MyBase.ProcessKeyPreview(m )
End If
Case Keys.Right
If _invalidRightKey = True Then
Return True
Else
Return MyBase.ProcessKeyPreview(m )
End If
End Select
'****************ProcessCm dKey
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Messa ge, ByVal keyData As System.Windows.Forms.Keys) As Boolean
If Not _dt Is Nothing Then
GetKeyStatus()
If (msg.Msg = WM_KEYDOWN Or (msg.Msg = WM_SYSKEYDOWN)) Then
'ignore the key if invalid edit entered
Select Case msg.WParam.ToInt32
Case Keys.Up
If _invalidUpKey = True Then
Return True
Else
'Return MyBase.ProcessKeyPreview(m )
Return MyBase.ProcessCmdKey(msg, keyData)
End If
Case Keys.Down
If _invalidDownKey = True Then
Return True
Else
'Return MyBase.ProcessKeyPreview(m )
Return MyBase.ProcessCmdKey(msg, keyData)
End If
Case Keys.Left
If _invalidLeftKey = True Then
Return True
Else
'Return MyBase.ProcessKeyPreview(m )
Return MyBase.ProcessCmdKey(msg, keyData)
End If
Case Keys.Right
If _invalidRightKey = True Then
Return True
Else
'Return MyBase.ProcessKeyPreview(m )
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Select
End If
End If
End Function
I have a custom datagrid, inherited from a windows form datagrid. The grid has rows and columns. I prevent the user from using the arrow keys (up, down, left, right) to go to certain rows or columns. I used the ProcessKeyPreview function of the datagrid to control the arrow keys in another project. In this project, I have more or less the same exact custom datagrid and the ProcessKeyPreview doesn't get fired at all. The strange thing is the ProcessCmdKey is what gets fired. Anyone experienced this? Below is the code. The same exact definition is in ProcessKeyPreview and ProcessCmdKey
**************ProcessKeyPr
Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Messa
If Not _dt Is Nothing Then
GetKeyStatus()
If (m.Msg = WM_KEYDOWN Or (m.Msg = WM_SYSKEYDOWN)) Then
'ignore the key if invalid edit entered
Select Case m.WParam.ToInt32
Case Keys.Up
If _invalidUpKey = True Then
Return True
Else
Return MyBase.ProcessKeyPreview(m
End If
Case Keys.Down
If _invalidDownKey = True Then
Return True
Else
Return MyBase.ProcessKeyPreview(m
End If
Case Keys.Left
If _invalidLeftKey = True Then
Return True
Else
Return MyBase.ProcessKeyPreview(m
End If
Case Keys.Right
If _invalidRightKey = True Then
Return True
Else
Return MyBase.ProcessKeyPreview(m
End If
End Select
'****************ProcessCm
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Messa
If Not _dt Is Nothing Then
GetKeyStatus()
If (msg.Msg = WM_KEYDOWN Or (msg.Msg = WM_SYSKEYDOWN)) Then
'ignore the key if invalid edit entered
Select Case msg.WParam.ToInt32
Case Keys.Up
If _invalidUpKey = True Then
Return True
Else
'Return MyBase.ProcessKeyPreview(m
Return MyBase.ProcessCmdKey(msg, keyData)
End If
Case Keys.Down
If _invalidDownKey = True Then
Return True
Else
'Return MyBase.ProcessKeyPreview(m
Return MyBase.ProcessCmdKey(msg, keyData)
End If
Case Keys.Left
If _invalidLeftKey = True Then
Return True
Else
'Return MyBase.ProcessKeyPreview(m
Return MyBase.ProcessCmdKey(msg, keyData)
End If
Case Keys.Right
If _invalidRightKey = True Then
Return True
Else
'Return MyBase.ProcessKeyPreview(m
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Select
End If
End If
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your response.
I was not handling the ProcessCmdKey method at all. I didn't even have a definition for it. All I had was the ProcessKeyPreview and it was not being fired. That's when I decided to try adding the ProcessCmdKey to see if that would work and it did.
The strange thing is that I used the ProcessKeyPreview to handle the arrow keys in a previous project and it worked fine. Whereas now, in this project it isn't being fired at all. ProcessCmdKey was not used in either project. So, it couldn't be the case that it was preventing the ProcessKeyPreview from being fired.
Thanks for your input though. I will read up more on both
San