Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Strange behaviour with ProcessKeyPreview and ProcessCmdKey

Posted on 2006-11-16
2
Medium Priority
?
852 Views
Last Modified: 2011-10-03
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

**************ProcessKeyPreview
 Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Message) 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


'****************ProcessCmdKey
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, 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

0
Comment
Question by:Sanmarie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
jake072 earned 1500 total points
ID: 17965580
If you read the following articles you will note that ProcessCmdKey PREPROCESSES messages i.e., it processes the messages before they are translated to the form, whereas ProcessKeyPreview handles messages received from the window.  ProcessCmdKey will always hook the event first, and if you handle the event in that method, then ProcessKeyPreview will never fire because the event was consumed in ProcessCmdKey.

Reference:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.processkeypreview.aspx
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.processcmdkey.aspx

Jake
0
 

Author Comment

by:Sanmarie
ID: 17967018

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



0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question