locked
<Tab> key does not raise "Enter" event for a control RRS feed

  • Question

  • When I use the <Tab> or <Shift/Tab> key to move to the next control, the control gets the focus, but the "Enter" event is not raised. I need to know when a control gets the focus so I can initialize variables as to the condition of this new control before the user can enter data.

    The "Enter" event is raised by all other navigation controls (up/down arrows, Home, End, etc.) by using the "KeyDown" event to trap these keys and coding the '.Focus' property for the appropriate next control. The "Enter" event is also raised OK by a mouse click. Only the the <Tab><Shift/Tab> keys do not raise this event, nor can I trap these keys with the "KeyDown" event.

    The VB Help for "Enter" specifically gives a list of focus actions which occur in which sequence when a control receives the focus, and the <Tab>, <Shift/Tab> are definitely included. I would appreciate any assistance anyone can give.

     

    Saturday, May 1, 2010 2:50 PM

Answers

  • Maybe you should use the GotFocus event instead of or in addition to the Enter event.

    However, I just tried the Enter event with a group of textboxes navigating with the Tab key, and it did work.  Here is my code:

    	Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter, TextBox2.Enter, TextBox3.Enter, TextBox4.Enter, TextBox5.Enter
    		Dim txt As TextBox = CType(sender, TextBox)
    		MessageBox.Show("You have entered " & txt.Name)
    	End Sub

    Solitaire
    Saturday, May 1, 2010 11:49 PM

All replies

  • When I use the <Tab> or <Shift/Tab> key to move to the next control, the control gets the focus, but the "Enter" event is not raised. I need to know when a control gets the focus so I can initialize variables as to the condition of this new control before the user can enter data.

    The "Enter" event is raised by all other navigation controls (up/down arrows, Home, End, etc.) by using the "KeyDown" event to trap these keys and coding the '.Focus' property for the appropriate next control. The "Enter" event is also raised OK by a mouse click. Only the the <Tab><Shift/Tab> keys do not raise this event, nor can I trap these keys with the "KeyDown" event.

    The VB Help for "Enter" specifically gives a list of focus actions which occur in which sequence when a control receives the focus, and the <Tab>, <Shift/Tab> are definitely included. I would appreciate any assistance anyone can give.

     

    How did you code it?
    Just Be Humble Malange!
    Saturday, May 1, 2010 5:21 PM
  • The code extracts are as follows (both these subroutines work for all but the <Tab> <ShiftTab> keys:

     1: the "Enter" event (<Tab> does not execute this subroutine)

    Private Sub txtRun1_Enter(ByVal sender As Object, ByVal e As SystemEventArgs) Handles txtRun1.Enter

      'user has focused on this field and it must be set up

      MatrixEntry()  'the common matrix field entry process

    End Sub

     2: the "KeyDown" event (<Tab> does not execute this subroutine either)

    Private Sub txtRun_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtRun1.KeyDown

      'take action if user pressed a specific control key or a control key combination

      Select Case True 'determine the common matrix navigation process

        Case e.Control And e.KeyCode = Keys.Down

          MatrixKeyDown_CtrlArrowDown() '- <Ctrl/down arrow>

                   ... "Case" code for <Ctrl/other key combinations follows here

        Case Else 'not a Ctrl/other key combination

          Select Case e.KeyCode

             Case Keys.Down '- <down arrow>

               MatrixKeyDown_lArrowDown() '- <down arrow>

             Case Keys.Escape '- <Esc>

               MatrixKeyDown_Escape() '- <Esc>

                  ... "Case"code for other single keys follows here

      End Sub

    Saturday, May 1, 2010 6:59 PM
  • Maybe you should use the GotFocus event instead of or in addition to the Enter event.

    However, I just tried the Enter event with a group of textboxes navigating with the Tab key, and it did work.  Here is my code:

    	Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter, TextBox2.Enter, TextBox3.Enter, TextBox4.Enter, TextBox5.Enter
    		Dim txt As TextBox = CType(sender, TextBox)
    		MessageBox.Show("You have entered " & txt.Name)
    	End Sub

    Solitaire
    Saturday, May 1, 2010 11:49 PM
  • Thank you. I adapted your solution to my code and it worked correctly.

    regards

    Richard Kirby

    Sunday, May 2, 2010 4:17 PM