Results 1 to 3 of 3

Thread: PreviewKeyDown Event Issue

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Location
    Milwaukee, Wisconsin, USA
    Posts
    133

    Question 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

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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
  •  



Click Here to Expand Forum to Full Width