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.LinkL abelLinkCl ickedEvent Args) Handles lnklblSupplier.LinkClicked
If frmLookupSuppliers.ShowDia log = Windows.Forms.DialogResult .OK Then
txtboxSuppCode.Text = frmLookupSuppliers.dgvLook up.Selecte dRows(0).C ells(0).Va lue
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
I have the following sub on a form of my project:
Private Sub lnklblSupplier_LinkClicked
If frmLookupSuppliers.ShowDia
txtboxSuppCode.Text = frmLookupSuppliers.dgvLook
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
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(keyDat a)
End Function
but am still having the same trouble. what am I doing wrong?
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(keyDat
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(keyDat a)
'check the keydata for enter key and return false...
retutn false
End Function
Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
Dim returnValue As Boolean
returnValue = Me.ProcessDialogKey(keyDat
'check the keydata for enter key and return false...
retutn false
End Function
ASKER
Ive tried that but when watching the function with a break point it never gets called?
ASKER
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.KeyEv entArgs) Handles dgvLookup.KeyDown
If e.KeyCode = Keys.Return Then
e.SuppressKeyPress = True
acceptSelection()
End If
End Sub
Private Sub dgvLookup_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEv
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.