Link to home
Start Free TrialLog in
Avatar of kiranboi
kiranboi

asked on

Ignoring a KeyDown Event on a DataGridView

Hi all,

I have the following sub on a form of my project:

Private Sub lnklblSupplier_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnklblSupplier.LinkClicked
        If frmLookupSuppliers.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtboxSuppCode.Text = frmLookupSuppliers.dgvLookup.SelectedRows(0).Cells(0).Value
        End If
    End Sub

When the linklabel is clicked it opens another form with a populated DataGridView on it. I have set on the DGV form that when a user double clicks the DGV it passes the value from the clicked row back to the main form and this works fine. However, I also want the user to be able to press the Return key and have the selected value be returned to the main form.
The problem I am having is that when the user presses return and value that is being passed back is the following row from the DGV because the return keypress is moving the selection down one. Is there any way I can stop the return keypress moving the selection of the DGV?

Thanks
Avatar of Jai S
Jai S
Flag of India image

http://www.developerfood.com/datagridview-use-enter-key-instead-of-the-tab-key/microsoft-public-dotnet-languages-vb/2863ea63-22e4-4eff-a4a6-a567b94b681e/article.aspx

it's right to override the ProcessDialogKey method in the DataGridView
subclass. ProcessDialogKey method is called in edit mode to handle keyboard
input that is not handled by the hosted editing control.

To sum up:
When the DataGridView is not in edit mode, we could handle the KeyPress
event of the DataGridView to capture key strokes.


We could also override the ProcessDataGridViewKey method in the
DataGridView subclass to get notified before the key stroke is sent to the
DataGridView.


When the DataGridView is in edit mode, we could handle the
EditingControlShowing event to get the hosted editing control in the
current cell and then subscribe the KeyPress event of the hosted editing
control in turn.


We could also override the ProcessDialogKey method in the DataGridView
subclass to get notified before the key stroke is sent to the DataGridView.
Avatar of kiranboi
kiranboi

ASKER

OK now youve confused me :op

Ive had a look at the link you sent and inserted the following into my code:

    Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
        Dim returnValue As Boolean
        returnValue = Me.ProcessDialogKey(keyData)
    End Function

but am still having the same trouble. what am I doing wrong?
just check whether enterkey is pressed ...and return false...

Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
        Dim returnValue As Boolean
        returnValue = Me.ProcessDialogKey(keyData)
'check the keydata for enter key and return false...
retutn false
    End Function
Ive tried that but when watching the function with a break point it never gets called?
Sorted it, I just changed my sub to the following instead. I forgot all about the SuppressKeyPress Property

Private Sub dgvLookup_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvLookup.KeyDown
        If e.KeyCode = Keys.Return Then
            e.SuppressKeyPress = True
            acceptSelection()
        End If
    End Sub
jst see whether your DGV works is EDIT mode - when you change some value and and press the enter kley the that value has to be commited...if it works then its great....
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial