locked
keystrokes carry over to other objects RRS feed

  • Question

  • Hi,

    I am facing a problem in my code. I am working on an application in vb.net, its a windows based application. On one of my form I need to perform so many data driven operations.

    if I press tab on one of the text box, I need to set a computed value in other text box and need to set the focus to that text box.

    I write a code that is a like to this.

        Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
            Select Case e.KeyCode
                Case Keys.Tab
                    MsgBox("Text1 Fired!")
    
                    Me.TextBox2.Text = "Set Value"
                    Me.TextBox2.Focus()
    
            End Select
        End Sub
    
        Private Sub TextBox2_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyUp
            Select Case e.KeyCode
                Case Keys.Tab
                    MsgBox("Text2 Fired!")
            End Select
        End Sub
    

     

    but when i press tab on textbox1, it doesn't set textbox2 value and gives me message that I have set on "TextBox2_KeyUp" event.

    also when i press "shift + Tab" on textbox2, it sets the textbox2 value and gives me message that i have set in "TextBox1_KeyUp" event.

    Also what are the keys which we can not catch in Keypress event. As when i try to do this from key press event it did not go there.

     

    ... any idea what could be the reason behind this??? Is there any other way by which I can handle this.

     

    Thanks in advance for your help!!!

     

     

    Friday, September 9, 2011 11:21 AM

Answers

  • The problem is not with the code but with the tab key itself

    Windows has a default setting that whenever the tab key is pressed the focus is transferred to the next control (according to the TAB order can be set from within the Visual studio), as the focus is transferred to the next control before the key up and key down event can be raised due to which the the code in key up event of the text box 1 is not executed instead text box 2 key up event is executed.

    Work arounds

    • use a different key or combination of keys (recommended )
    • set an override to TAB key press (not tested and not recommended )

    Protected Overrides Function ProcessTabKey( _
    ByVal forward As Boolean _
    ) As Boolean
    MyBase.ProcessTabKey(forward)
    MsgBox("Tab key was pressed!")
    End Function
    



    Please mark those posts as answer which answers your question. Faraz
    • Proposed as answer by Mike Feng Monday, September 12, 2011 9:20 AM
    • Marked as answer by Mike Feng Sunday, September 18, 2011 7:09 AM
    Friday, September 9, 2011 12:14 PM
  • 1. Tab order cannot be used because there is some code to be executed when the focus is transfered.

    Yes, that's done in the Leave event.

    2. The same reason as above

    Would be covered by handling the Leave event but not if the Tab key is handled only.

    Or do I overlook something?


    Armin
    • Proposed as answer by Mike Feng Monday, September 12, 2011 9:21 AM
    • Marked as answer by Mike Feng Sunday, September 18, 2011 7:09 AM
    Friday, September 9, 2011 5:08 PM

All replies

  • The problem is not with the code but with the tab key itself

    Windows has a default setting that whenever the tab key is pressed the focus is transferred to the next control (according to the TAB order can be set from within the Visual studio), as the focus is transferred to the next control before the key up and key down event can be raised due to which the the code in key up event of the text box 1 is not executed instead text box 2 key up event is executed.

    Work arounds

    • use a different key or combination of keys (recommended )
    • set an override to TAB key press (not tested and not recommended )

    Protected Overrides Function ProcessTabKey( _
    ByVal forward As Boolean _
    ) As Boolean
    MyBase.ProcessTabKey(forward)
    MsgBox("Tab key was pressed!")
    End Function
    



    Please mark those posts as answer which answers your question. Faraz
    • Proposed as answer by Mike Feng Monday, September 12, 2011 9:20 AM
    • Marked as answer by Mike Feng Sunday, September 18, 2011 7:09 AM
    Friday, September 9, 2011 12:14 PM
  • Hi Ashish,

    Try this code, it works and let me know if it is working.

     

    Protected Overrides Function ProcessTabKey(ByVal forward As Boolean) As Boolean
    
            If TextBox1.Focused Then
                MsgBox("Text1 Fired!")
    
                Me.TextBox2.Text = "Set Value"
                Me.TextBox2.Focus()
            End If
    
            Return MyBase.ProcessTabKey(forward)
    
        End Function
    

    Hope it helps.

    Regards,

    A.Murugan.
     


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    Friday, September 9, 2011 12:34 PM
  • Protected Overrides Function ProcessTabKey(ByVal forward As Boolean) As Boolean
    
            If TextBox1.Focused Then
                MsgBox("Text1 Fired!")
    
                Me.TextBox2.Text = "Set Value"
                Me.TextBox2.Focus()
            End If
    
            Return MyBase.ProcessTabKey(forward)
    
        End Function
    

    Hope it helps.

    Regards,

    A.Murugan.


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!

    Hello Murugan

    Have you tested the code ?

    It looks like that for some reasons the focus can not be transferred to the second text box. If it works for you, pleas tell if you have change any other settings.

     


    Please mark those posts as answer which answers your question. Faraz
    Friday, September 9, 2011 12:46 PM
  • Hi Faraz,

    Yes i have tested the code by myself with two sample textboxes and i didn't change any default settings. Are you facing any problems with it. If so what is it?

    Looking forward to hear from you.

    Regards,

    A.Murugan.


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    Friday, September 9, 2011 1:03 PM
  • This is the code which i have on my form

    Protected Overrides Function ProcessTabKey(ByVal forward As Boolean) As Boolean
    
            If TextBox1.Focused Then
                MsgBox("Text1 Fired!")
    
                Me.TextBox2.Text = "Set Value"
                Me.TextBox2.Focus()
    
            
            End If
    
            Return MyBase.ProcessTabKey(forward)
    
        End Function
        Private Sub TextBox2_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyUp
            If (e.KeyCode = Keys.Tab) Then
                MessageBox.Show("Text2 Fired!")
    
            End If
        End Sub
    

    The form contains two text boxes. when the TAB key is pressed the cursor should move to the textbox2 but it does not , however the text of the textbox2 changes.


    Please mark those posts as answer which answers your question. Faraz
    Friday, September 9, 2011 1:57 PM
  • I don't know if you want to handle it in two steps, first the setting and than the focus. 

    But this code works, I agree with you that what happens in your original code looks weird.

        Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles TextBox1.PreviewKeyDown
            Select Case e.KeyCode
                Case Keys.Tab
                    Me.TextBox2.Text = "Set Value"
            End Select
        End Sub
        Private Sub TextBox1_KeyUp1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
            Select Case e.KeyCode
                Case Keys.Tab
                    Me.TextBox2.Focus()
            End Select
        End Sub
    
    



    Success
    Cor
    Friday, September 9, 2011 2:28 PM
  • Some thoughts:

    • Why don't you just set the taborder correctly so that textbox2 comes after textbox1?
    • What if you leave the textbox by using the mouse?
    • Showing a Msgbox while testing focus issues was always a very bad idea. Use Debug.Writeline instead.

    Armin
    Friday, September 9, 2011 4:16 PM
  • Some thoughts:

    • Why don't you just set the taborder correctly so that textbox2 comes after textbox1?
    • What if you leave the textbox by using the mouse?
    • Showing a Msgbox while testing focus issues was always a very bad idea. Use Debug.Writeline instead.

    Armin
    1.  Tab order cannot be used because there is some code to be executed when the focus is transfered.
    2.  The  same reason as above
    3.  Yes it Debug.writeline works , Thanks.
      Protected Overrides Function ProcessTabKey(ByVal forward As Boolean) As Boolean
              If TextBox1.Focused Then
                  MsgBox("Text1 Fired!")
                  Me.TextBox2.Text = "Set Value"
                  Debug.WriteLine(TextBox2)
              End If
      
              Return MyBase.ProcessTabKey(forward)
      
          End Function
          Private Sub TextBox2_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyUp
              If (e.KeyCode = Keys.Tab) Then
                  MessageBox.Show("Text2 Fired!")
              End If
          End Sub
      

     


    Please mark those posts as answer which answers your question. Faraz
    Friday, September 9, 2011 4:49 PM
  • 1. Tab order cannot be used because there is some code to be executed when the focus is transfered.

    Yes, that's done in the Leave event.

    2. The same reason as above

    Would be covered by handling the Leave event but not if the Tab key is handled only.

    Or do I overlook something?


    Armin
    • Proposed as answer by Mike Feng Monday, September 12, 2011 9:21 AM
    • Marked as answer by Mike Feng Sunday, September 18, 2011 7:09 AM
    Friday, September 9, 2011 5:08 PM