• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2245
  • Last Modified:

Disable Enter key in Datagrid

I have a Datagrid with custom columns. These columns contain textboxes but also comboboxes and checkboxes. Now when a user presses the enter button when editing the values in a textfield column then a Datagrid event will be fired. This is something I totally not want. Is there a way to ignore the Enter key in a Datagrid? If I press enter in a textboxfield then the next row will be selected. I tried to catch the Enter key in the textboxes but it seems like they are overruled by this Datagrid event. Any idea to fix this problem?
0
paing0d
Asked:
paing0d
  • 3
  • 3
1 Solution
 
Jeffr0Commented:
Maybe a custom DataGrid will solve this:

Put this code in the same namesapce as your Form:

Public Class Jeffr0sDataGrid
    Inherits DataGrid

    Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Message) As Boolean
        If m.Msg = 256 Then
            Return False
        End If

        Return True
    End Function
End Class



Then change the code that instantiates the Grid like this:


        Me.DataGrid1 = New Jeffr0sDataGrid()

(The code for it is in the " Windows Form Designer generated code " area.)
0
 
Jeffr0Commented:
My solution has a bonus feature in that it also ignores the arrow keys except for when you're inside the datagrid cell.  You can only use the tab key and mouse to navigate.

:)
0
 
paing0dAuthor Commented:
If I press enter the first time in my self made textfield column it does jump to the next row. If I keep pressing enter it stays there but does not go further. So I think it works after a key is pressed the first time? On every row it jumps to the next row when pressed the enter key (in the custom textfield column) but goes no further.

If I press other keys then Enter it blocks them too. I can't typ in my own made textfield columns anymore.
0
Technology Partners: 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!

 
Jeffr0Commented:
Oh well.  That's the best I can do for a 20 point question.  :)

It works pretty good when you don't have any custom columns, though, doesn't it??
0
 
paing0dAuthor Commented:
This did the trick:

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If (msg.WParam.ToInt32 = 13) Then
            Return True
        End If

        Return False
    End Function

Thanks for guiding me in the right direction.
0
 
paing0dAuthor Commented:
And if you want to filter out the arrow keys too just make it:

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case msg.WParam.ToInt32
            Case 13, 37, 38, 39, 40
                Return True
        End Select

        Return False
    End Function
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now