Solved

Disable Enter key in Datagrid

Posted on 2004-04-28
6
2,108 Views
Last Modified: 2007-12-19
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
Comment
Question by:paing0d
  • 3
  • 3
6 Comments
 
LVL 2

Accepted Solution

by:
Jeffr0 earned 20 total points
ID: 10941587
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
 
LVL 2

Expert Comment

by:Jeffr0
ID: 10942191
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
 

Author Comment

by:paing0d
ID: 10943510
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:Jeffr0
ID: 10943597
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
 

Author Comment

by:paing0d
ID: 10943603
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
 

Author Comment

by:paing0d
ID: 10943611
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now