Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

ControlCOntainer Question

ControlCOntainer QUestion

I am trying to make the enter key act like the tab key.  I.E. my user wants to have the enter key move the input focus to the next field / TextBox.  I figured out how to do this when the controls are bound directly to the form but could not figure out how to do it when the textbox's where bound to a panel/tabPage.


Here is the code that works when the textbox's are bound directly to a form. 


  '############################################
  '## Find the control with the input focus
  '############################################
  Private Function findFocus() As Control
      Dim myControl As Control
      Dim myControlCollection As ControlCollection = Me.Controls

      For Each myControl In myControlCollection
        If myControl.ContainsFocus() Then
            Return myControl
        End If
      Next
  End Function

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

      '#####################################################################
      '## Make the Enter Key act like the Tab Key
      '  ## On Enter go to the next control in the tab order
      '  ## On Enter AND Shift got to the previous control in the tab order
      '#####################################################################
      If e.KeyCode = Keys.Enter Then
        Dim c As Control = Me.findFocus()
        If e.Shift Then  '**Go Back
            Me.SelectNextControl(c, False, True, True, True)
        Else              '**Go foward
            Me.SelectNextControl(c, True, True, True, True)
        End If
      End If

  End sub



The real problem is in the findFocus function.  When I put the textbox's on a tabPage the function returns the TabControl when what I really am looking for is the textbox on the tabcontrol/tabpage with the input focus.

DanP
Thursday, November 13, 2003

Don't do this. You're breaking the common UI model of Windows.

Brad Wilson (dotnetguy.techieswithcats.com)
Thursday, November 13, 2003

Your user is wrong -- just tell them to use the tab key!

If they insist, then your findFocus() method looks as though it needs a little recursion to traverse down the tree of controls, something like:


Private Sub Form1_KeyDown(...)
  Dim c As Control = findFocus(Me) 'start at top level
  ...
End Sub


Private Function findFocus(parentControl As Control) As Control
    Dim childControl As Control
   
    For Each childControl In parentControl.Controls   
        If childControl.ContainsFocus() Then
            Dim grandChild As Control
            grandChild = findFocus(childControl)
            If (grandChild Is Nothing) Then
                Return childControl
            Else
                Return grandChild
            End If
        End If
    Next
End Function

Duncan Smart
Friday, November 14, 2003

Thanks Duncan.  Your code worked great.  Your a life saver.

I didn't want to make this change for the user either, but she showed me why she wanted the change.  At the end of the record the last two fields are numeric and her right hand is on the keypad while her left is flipping to the next ticket.  Basically, neither hand is near the tab key, but the right hand is near the enter key.  So it's much faster to use the enter key.  She enters 1000+ tickets a day so a little keyboard trick can save her a lot of time.

Plus she promised to enter alot of test data for me if I made the change.  Need I say more?

Thanks again for the code

DanP
Friday, November 14, 2003

Sounds like the worthwhile compromise Dan!

Duncan Smart
Friday, November 14, 2003

*  Recent Topics

*  Fog Creek Home