Setting focus to control in next/previous tabpage?

Hi,

I use a TabControl several places in my project, and this is what I want to do:

When the user presses tab in the last control on a tabpage, I want to set focus to the first control in the next tabpage.

When the user presses Shift+Tab in the first control on a tabpage, I want to set focus to the last control in the previous tabpage.

If there's no next or previous tabpage, I want to follow the "normal" taborder.

Is there a pretty way to do this that I have yet to discover



Answer this question

Setting focus to control in next/previous tabpage?

  • Jim King

    I don't necessarily recommend this, because it makes your UI non-standard. For instance, if there are other controls on the form besides the tab control, then you won't be able to tab to them.

    However, if you really want to do it, one way is to override the form's handling of ProcessTabKey...  Place the following code in the form that contains the tab control:

        Protected Overrides Function ProcessTabKey(ByVal forward As Boolean) As Boolean
            If ActiveControl IsNot Nothing AndAlso TypeOf ActiveControl.Parent Is TabPage Then
                'Current control is on a tab page
                Dim currentTabPage As TabPage = CType(ActiveControl.Parent, TabPage)
                Dim nextControl As Control = GetNextControl(ActiveControl, forward)
                If nextControl IsNot Nothing AndAlso nextControl.Parent IsNot currentTabPage Then
                    'The next control in the tab sequence is not on the same page, i.e., it's the last
                    '  (or first) control on the page.  Switch to the next page
                    Dim tabControl As TabControl = TryCast(currentTabPage.Parent, TabControl)
                    If tabControl IsNot Nothing Then
                        Dim nextTabIndex As Integer
                        If forward Then
                            If tabControl.SelectedIndex < tabControl.TabCount - 1 Then
                                'Next tab page
                                nextTabIndex = tabControl.SelectedIndex + 1
                            Else
                                'We're already on the last tab page, move to the first
                                nextTabIndex = 0
                            End If
                        Else
                            If tabControl.SelectedIndex > 0 Then
                                'Previous tab page
                                nextTabIndex = tabControl.SelectedIndex - 1
                            Else
                                'Already on the first tab page, move to the last
                                nextTabIndex = tabControl.TabCount - 1
                            End If
                        End If

                        tabControl.SelectTab(nextTabIndex)
                        If Not forward Then
                            'If we just switched pages and are going backward (Shift+TAB), then
                            '  we need to select the last control on that page instead of the
                            '  first.
                            tabControl.SelectNextControl(Nothing, forward, True, True, True)
                        End If

                        'Let Windows Forms know we processed this tab key
                        Return True
                    End If
                End If
            End If

            'Let Windows Forms handle tab normally
            Return MyBase.ProcessTabKey(forward)
        End Function



  • lwmorris067

    Hi,

    There is no pretty way of doing this. You'll have to detect if its the last control and just sendkey a "ctrl+tab" to move to the other tabpage...

     

     

    cheers,

    Paul June A. Domag



  • Setting focus to control in next/previous tabpage?