|
-
Mar 22nd, 2012, 05:42 PM
#1
Thread Starter
Addicted Member
PreviewKeyDown Event Issue
Below is code I have written to capture when the tab key is pressed inside of a two-column data grid view. The only problem I'm having is that I have to press the tab key twice for this event to fire. Would anybody know why this is. Any help you can provide is greatly appreciated...
Code:
Private Sub dgvCustomerIncludes_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles dgvCustomerIncludes.PreviewKeyDown
If e.KeyValue = 9 Then
For X As Integer = 0 To (dgvCustomerIncludes.Rows.Count - 1)
If Not Trim(dgvCustomerIncludes.Rows(X).Cells(0).Value) = "" And Trim(dgvCustomerIncludes.Rows(X).Cells(1).Value) = "" Then
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' customer id with no customer name so call function GetCustomerName '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dgvCustomerIncludes.Rows(X).Cells(1).Value = GetCustomerName(Trim(dgvCustomerIncludes.Rows(X).Cells(0).Value))
dgvCustomerIncludes.Refresh()
ElseIf Trim(dgvCustomerIncludes.Rows(X).Cells(0).Value) = "" And Not Trim(dgvCustomerIncludes.Rows(X).Cells(1).Value) = "" Then
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' customer id was deleted but name is still there so erase the customer name '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dgvCustomerIncludes.Rows(X).Cells(1).Value = ""
dgvCustomerIncludes.Refresh()
End If
Next X
End If
End Sub
-
Mar 22nd, 2012, 05:49 PM
#2
Re: PreviewKeyDown Event Issue
I haven't delved too much into this so I can't say off the top of my head whats wrong with your code, but Jmc has a great blog post about keyboard events and how they relate to .Net control key events. Read this, it may help you.
Last edited by Niya; Mar 22nd, 2012 at 06:10 PM.
-
Mar 22nd, 2012, 07:05 PM
#3
Re: PreviewKeyDown Event Issue
First things first, why would you use KeyValue rather than KeyData? The value 9 means nothing to me and probably to most people and you have no comment to indicate what it means. If you were to use KeyData then you'd have a Keys value instead of a number and it would be self-documenting.
As for the issue, I'm not sure that this is it but you have to consider that a child control is embedded in a cell when it's being edited and not other times. As such, maybe the editing control is receiving the first key press and the grid is receiving the second. You may need to handle the appropriate event(s) of the editing control.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|