?
Solved

Keyevents fires twice

Posted on 2005-03-11
20
Medium Priority
?
1,586 Views
Last Modified: 2008-01-16
Gurus,

After installing service pack 1 for .Net framework version 1.1 i am having very strange behaviour. All buttons and list box is getting keyup events twice. I haven't checked other controls and key events.

Also my form is always receving key events first i believe the controls receives events first and then form. But after installing SP1 this has changed. Not sure its because of SP1 or something else.

In below listbox keyup event i am opening other modal form on which if i press enter key the list box is getting that enter..

    Private Sub lboCallCustomer_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lboCallCustomer.KeyUp
        'If lboKeyFlg Then
        '    lboKeyFlg = False
        '    Exit Sub
        'End If
        Dim ee As New System.EventArgs
        If e.KeyCode = Keys.Return Then
            HammerAction()
        End If
        'lboKeyFlg = True
    End Sub

Below is keyup for forms which always receive events first even if focus is in listbox.

    Private Sub frmHammerWork_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        Select Case e.KeyCode
            Case Keys.F1
                Hammerbtn()
            Case Keys.F2
                ACHBtn()
            Case Keys.F3
                DraftBtn()
             Case Keys.F4
                LogBtn()
             Case Keys.Escape
                Close()
         End Select
    End Sub

Any ideas? Am I doing something wrong?

Any suggestions are appriciated.

Thanks.

"Always confused after downloading hotfixes"


0
Comment
Question by:coolrajm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 6
  • 4
20 Comments
 
LVL 27

Assisted Solution

by:planocz
planocz earned 200 total points
ID: 13516459
Most of the time your key strokes are regulated basically starting from the keypress event,
If you have a special need you use the keypress up and keypress down.
Also if you want to check your keys right up front then you could use this as a front line of contact.....

   Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        Const WM_KEYDOWN As Integer = &H100
        Const WM_SYSKEYDOWN As Integer = &H104

        Try
            If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
                Select Case (keyData)
                    Case Keys.Enter
                        SendKeys.Send("{TAB}")
                    Case Keys.F9
                       'DO SOMETHING
                    Case Keys.Right, Keys.Left, Keys.Down, Keys.Up, Keys.Tab
                       'DO SOMETHING
                    Case Keys.A To Keys.Z
                    Case Keys.NumPad0 To Keys.NumPad9, Keys.D0 To Keys.D9
                    Case (Keys.Control Or Keys.M)
                        '   Me.Parent.Text = "<CTRL> + M Captured"
                    Case (Keys.Alt Or Keys.Z)
                        '  Me.Parent.Text = "<ALT> + Z Captured"
                End Select
            End If
        Catch ex As InvalidCastException
            'Skip DBNULL
        Catch ex As SystemException
            MsgBox(ex.StackTrace & ex.Message, MsgBoxStyle.Critical, "General Error")
        End Try
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
0
 

Author Comment

by:coolrajm
ID: 13516560
Do you mean i need to do this on each form?

Thanks for your help.
0
 
LVL 27

Expert Comment

by:planocz
ID: 13516648
yes, each form is independent.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:coolrajm
ID: 13516719
Thanks.
Thats too much work as we are almost at finish line with project and we just updated one of our development computer with SP1 of .net framwork and we started facing such strange behaviour.

Anyway thanks. I will test it by today and will post it results.

Also can you please tell me why form is getting events first instead of control where the focus is. I am saying this as evenif focus in combo box my form is getting key events first then combo. Previously it was not doing that way.

Thanks again for promptly reply.
0
 
LVL 27

Expert Comment

by:planocz
ID: 13516795
check your tab indexes so that tab 0 will equal your first focus control.
0
 

Author Comment

by:coolrajm
ID: 13516916
I didn't understand what you want to say here. But combobx is the first control on my form and tab index set for this is 0.

I have two buttons on form with 'o' key is assigned to one button and 'c' key is assigned to another button. But as c and o are not control keys i need these key to be type in combobox when focus in combobox but its not doing that. When i am typing c in combobox my form is getting key event first and its doing work assigned to button which has c assigned to it.

Hope u understand my problem. Although i can assign function keys to my all buttons but this will led to big work.

Thanks.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13517038
Hi coolrajm,

For this sub to work with your form no matter what control has the focus:

    Private Sub frmHammerWork_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        Select Case e.KeyCode
            Case Keys.F1
                Hammerbtn()
            Case Keys.F2
                ACHBtn()
            Case Keys.F3
                DraftBtn()
             Case Keys.F4
                LogBtn()
             Case Keys.Escape
                Close()
         End Select
    End Sub

Your forms KeyPreview property must be set to true.  When KeyPreview is True, the form is supposed to get the keypress events BEFORE the currently focused control.  If it wasn't doing this before, then the service pack fixed that and your app is working the way it's supposed to now.

From the Help File on KeyPreview:
"Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus."

~IM
0
 

Author Comment

by:coolrajm
ID: 13517198
Thank you so much. Thats what i want to know...

I think i am dumb......didn't went to keypreview help even once. The basic knowledge i have is control should get focus first so i thought its always that whether keypreview is true or not. Good to know that SP1 fixed my problem.

Thanks a thousand.

Can you please greater my dumbness with below?

In below listbox keyup event i am opening other modal form on which if i press enter key (Enter key is closing my modal form) the list box is getting that enter key and its opening the modal form again..

    Private Sub lboCallCustomer_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lboCallCustomer.KeyUp
        'If lboKeyFlg Then
        '    lboKeyFlg = False
        '    Exit Sub
        'End If
        Dim ee As New System.EventArgs
        If e.KeyCode = Keys.Return Then
            HammerAction()
        End If
        'lboKeyFlg = True
    End Sub

Thanks again,

Transferred to VB .NET by express path way (Without vb) from JAVA
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13517274
With what you have shown us, the HammerAction() sub should be only called once for each Enter keypress:

    Private Sub lboCallCustomer_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lboCallCustomer.KeyUp
        If e.KeyCode = Keys.Return Then
            HammerAction()
        End If
    End Sub

Do you perhaps have something in lboCallCustomer.KeyDown() or lboCallCustomer.KeyPress()?
0
 

Author Comment

by:coolrajm
ID: 13517358
No i haven't.

i want whenever focus is in listbox and keypress is enter it should call HammerAction() which is opening one modal form.

I haven't implemented any other event i.e keydown and keypress for listbox. Although i have implemented doubleclick for listbox which is also calling Hammeraction() but i don't think thats the issue as that is mouse event.

But still whenever i press enter key on open modal form its opening the modal form again as keyup of listbox is firing again.

Thanks again.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13518148
With this:

    Private Sub lboCallCustomer_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lboCallCustomer.KeyUp
        If e.KeyCode = Keys.Return Then
            Debug.WriteLine("HammerAction()")
        End If
    End Sub

I am only getting one "HammerAction()" in my output window per Enter keypress.  Something else is going on in your app.

What is inside HammerAction()?
0
 

Author Comment

by:coolrajm
ID: 13518221
Calling below function from HammerAction. Which opens the modal form on which when i am pressing enter key it closes that modal form but once it closed my listbox is getting keyup again so its opening this form again.
   
 Private Sub ShowIncrDecrForm(ByVal Action As Long)
        Dim i As Integer = 0
        Dim frm As New frmIncrDecrAmt

        frm.RIID = drRI("RI_Id")
        frm.drCustomer = drRI
        frm.Action = Action
        frm.CollType = CollectionsType.Hammer
        frm.ShowDialog()
  End Sub

Thanks.
0
 
LVL 27

Expert Comment

by:planocz
ID: 13518251
If you use this...  ProcessCmdKey Overrides all keypress's and you will not need any others.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13518258
So when frmIncrDecramt is open you press the enter key to close it?

Show me that code...
0
 

Author Comment

by:coolrajm
ID: 13518267
May be you can remove hammeraction() and just add MsgBox() and in msgbox press enter you will see the box again. That is what i am getting.

Thanks.
0
 

Author Comment

by:coolrajm
ID: 13518308
planocz you might be right and i appriciated your inputs but we have this issue with many screens and i just don't won't to double the work if i am doing something wrong at my end.

Thanks again.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13518325
Ok...replaced

    Debug.WriteLine("HammerAction()")

with
     
    MsgBox("HammerAction()")

and I see your problem.

If you use KeyPress() instead of KeyUp() then that doesn't happen:

    Private Sub lboCallCustomer_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles lboCallCustomer.KeyPress
        If Asc(e.KeyChar) = Keys.Return Then
            MsgBox("HammerAction()")
        End If
    End Sub
0
 

Author Comment

by:coolrajm
ID: 13518348

 IM:

 I have 4 textboxes on that form i am passing some values in texbox with TAB and when my TAB is on OK button i am pressing enter key. In OK button i have just written close().

Hope that helps.

Thanks.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 13518429
Changing from KeyUp() to KeyPress() should fix that problem:

    Private Sub lboCallCustomer_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles lboCallCustomer.KeyPress
        If Asc(e.KeyChar) = Keys.Return Then
            HammerAction()
        End If
    End Sub
0
 

Author Comment

by:coolrajm
ID: 13518482
You right. Thanks a lot....But do you have any idea why is that? Changing that is not difficult.

Thanks.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 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