The Switching event is invoked whenever a new tab is about to become active.
The main function of this event is to validate any input data on the current page (about to be replaced by a new page). The Allow parameter can be set to True to allow the next page to become active or False to stay on the current page.
If a message box is displayed or the input focus shifts away from the tab control while the Switching event is being processed, tab switching is aborted and the current tab remains the active tab (i.e. Allow is implicitly set to False and any value specified by the application is ignored).
In most cases, this default processing is exactly what is intended. However, there may be cases where the new tab should become active (even if validation fails). For example, a message box is displayed such as "Are you sure?" or similar. The message box (by definition) cancels tab switching. After the message box, setting Allow to True has no effect, as tab switching has already been canceled (due to the input focus shifting away from the tab control).
There is an easy way to "force" the tab control to switch to the new tab. The key is to perform the switch after the Switching event has completed (of course, the "old" tab page is still displayed). This can be easily accomplished using a Timer control. In the Switching event, a timer is set which will fire as soon as tab switching (the Switching event) has ended. The following is a sample Switching event. A timer control (Timer1) has been added to the form. Some properties are explicitly set at run-time to show that they are being used. These can of course also be set at design-time.
Option Explicit
Private Sub Form_Load()
' disable the timer
Timer1.Enabled = False
' make it a very fast timer
Timer1.Interval = 1
' mark the Tag property as "no next page"
Timer1.Tag = -2
End Sub
Private Sub SftTabs1_Switching(NextTab As Integer, Allow As _ Boolean, Refresh As Boolean)
Dim result As Integer
' check if this is a Switching event caused by the timer event
If Timer1.Tag < -1 Then
' The user is switching the tab page
result = MsgBox("Are you sure?", vbYesNoCancel)
' tab switching has just been canceled because of the message
' box
' now analyze the user's response, we may have to explicitly ' switch
If result = vbYes Then
' The following won't work, as the Current property is
' not available during the Switching event
'SftTabs1.Tabs.Current = NextTab
' SftTabs/OCX 3.0: the CurrentTab property also cannot
' be used
' Set up the timer, so we can switch right after this
' event ends save the next tab index in the Tag property
Timer1.Tag = NextTab
' enable the timer
Timer1.Enabled = True
Else
' actually already cancelled, this really isn't needed
' for SftTabs/ATL 3.5
' however, if you're using SftTabs/OCX 3.0 you must set
' Allow to False
Allow = False
End If
Else
' The timer event is causing this tab switch
' simply allow it
Allow = True
End If
End Sub
Private Sub Timer1_Timer()
' we need to switch to a new tab
' disable the timer so it won't happen again
Timer1.Enabled = False
' switch to the new tab, we saved the index in the Tag property
' use CurrentTab for SftTabs/OCX 3.0
SftTabs1.Tabs.Current = Timer1.Tag
' we're done switching, "forget" the next tab
Timer1.Tag = -2
End Sub
|