Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1448

Fix for SSTab not correctly handling focus and comboboxes

$
0
0
Make a Class module out of the following code, create an object with it for any form that uses the SSTab control and follow directions in comments.

Code:

'
' ****************************************************************
' ****************************************************************
' ****************************************************************
' This program is free software: you can redistribute it and/or
' modify it under the terms of the GNU General Public License
' version 3 as published my the Free Software Foundation:
' http://www.gnu.org/licenses/gpl.html
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' This software was originally written by Elroy Sullivan, PhD in
' cooperation with Shriners Hospitals for Children, Houston.
' Dr. Sullivan can be reached at elroysullivan@gmail.com.
' ****************************************************************
' ****************************************************************
' ****************************************************************
'
Option Explicit
'
Dim WithEvents tabCtl As SSTab
Dim frm As Form
Dim TabStops() As Boolean
'
' A primary purpose of this fix is to correctly control the tab stops.
' To make the appearance of tabs, the SSTab control simply moves the controls out of view.
' An artifact of this approach is that the controls are still able to get the focus when the
' user uses the TAB key.  The following code corrects this problem by appropriately turning
' on and off the TabStop properties of the controls as the user tabs from one tab to another.
'
' Another problem has to do with ComboBoxes.  When changing to a new tab, dropdown comboboxes
' will have their text selected.  The combobox will not have the focus, but their text will be
' selected.  The primary problem with this is that it right-justifies the text when there is more
' text than will fit in the textbox portion of the combobox, and this is confusing to users.
' This problem is corrected in the following code.
'

Friend Sub SetTabControl(TheTabControl As SSTab, TheForm As Form)
    ' Call this in the form load event.
    Dim ctl As Control
    Dim ptr As Long
    '
    Set tabCtl = TheTabControl
    Set frm = TheForm
    '
    ' Store the true value of the TabStops.
    ReDim TabStops(0 To frm.Controls.Count - 1)
    ' Not all controls have TabStop property, so we must set error trapping.
    On Error Resume Next
    For ptr = 0 To frm.Controls.Count - 1
        TabStops(ptr) = frm.Controls(ptr).TabStop
    Next ptr
    On Error GoTo 0
End Sub

Friend Sub SetTabStopsAccordingly()
    ' Call this in the form activate event.
    ' After this first call, it will automatically be called when the tabs change.
    Dim ctl As Control
    Dim ctlTheControlOrContainer As Control
    Dim ItsOnTheTabControl As Boolean
    Dim ptr As Long
    '
    For ptr = 0 To frm.Controls.Count - 1
        Set ctl = frm.Controls(ptr)
        Set ctlTheControlOrContainer = ctl ' The control might be on a container that's on the SSTab, rather than directly on the SSTab.
        Do
            Select Case True
            Case TypeOf ctlTheControlOrContainer.Container Is SSTab
                ItsOnTheTabControl = True
                Exit Do ' The way out.
            Case TypeOf ctlTheControlOrContainer.Container Is Form
                ItsOnTheTabControl = False
                Exit Do ' The way out.
            End Select
            Set ctlTheControlOrContainer = ctlTheControlOrContainer.Container ' Must deal with controls nested deeper than the SSTab control.
        Loop
        If ItsOnTheTabControl Then
            ' Not all controls have TabStop property, so we must set error trapping.
            On Error Resume Next
            If ctlTheControlOrContainer.Left >= 0 Then
                ctl.TabStop = TabStops(ptr) ' If it's showing, restore the original TabStop value.
                ' Must also fix the problem with combo boxes having an internal focus set.
                ctl.SelStart = 0
                ctl.SelLength = 0
            Else
                ctl.TabStop = False
            End If
            On Error GoTo 0
        End If
    Next ptr
End Sub

Private Sub tabCtl_Click(PreviousTab As Integer)
    SetTabStopsAccordingly
End Sub

Private Sub tabCtl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ' This allows other controls to close up when user click off.
    ' The problem is that clicking into the body of the tab control does NOT cause change in focus.
    ' The control with the focus keeps it, and it may not close up as typically happens when clicking on dead space of a form.
    ' You may also want to consider placing this "SetFocus" code on the labels on the tabs.  This is NOT automatically done
    ' because the programmer may want to use a label click for other purposes.
    tabCtl.SetFocus
End Sub


Viewing all articles
Browse latest Browse all 1448

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>