[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1678
  • Last Modified:

Keyevents fires twice

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
coolrajm
Asked:
coolrajm
  • 10
  • 6
  • 4
2 Solutions
 
planoczCommented:
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
 
coolrajmAuthor Commented:
Do you mean i need to do this on each form?

Thanks for your help.
0
 
planoczCommented:
yes, each form is independent.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
coolrajmAuthor Commented:
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
 
planoczCommented:
check your tab indexes so that tab 0 will equal your first focus control.
0
 
coolrajmAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
coolrajmAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
coolrajmAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
coolrajmAuthor Commented:
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
 
planoczCommented:
If you use this...  ProcessCmdKey Overrides all keypress's and you will not need any others.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So when frmIncrDecramt is open you press the enter key to close it?

Show me that code...
0
 
coolrajmAuthor Commented:
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
 
coolrajmAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
coolrajmAuthor Commented:

 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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
coolrajmAuthor Commented:
You right. Thanks a lot....But do you have any idea why is that? Changing that is not difficult.

Thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now