BeforeClick Event Example

This example uses the BeforeClick event to demonstrate how to prevent a user from switching to another tab. This is useful when you want to verify information on the current tab before displaying the newly selected tab.

To try this example, place a TabStrip control and a two-element Frame control array on the form (set the BorderStyle properties to None). In the first Frame control, add a CheckBox control and in the second, add a TextBox. Paste the following code into the Load event of the Form object, and run the program. Click the tab labeled Text after you select/deselect the CheckBox on the tab labeled Check.

Private Sub Form_Load()
Dim i As Integer
Dim Tabx As Object
' Sets the caption of the first tab to "Check."
TabStrip1.Tabs(1).Caption = "Check"
' Adds a second tab with "Text" as its caption.
Set Tabx = TabStrip1.Tabs.Add(2, , "Text")
' Labels the checkbox.
Check1.Caption = "Cancel tab switch"
   ' Aligns the Frames with the internal area
   ' of the Tabstrip Control.
   For i = 0 To 1
      Frame1(i).Left = TabStrip1.ClientLeft
      Frame1(i).Top = TabStrip1.ClientTop
      Frame1(i).Height = TabStrip1.ClientHeight
      Frame1(i).Width = TabStrip1.ClientWidth
   Next
   ' Puts the first tab's Frame container on top.
   Frame1(0).ZOrder 0
End Sub

' The BeforeClick event verifies the check box value 
' to determine whether to proceed with the Click event.
Private Sub TabStrip1_BeforeClick(Cancel As Integer)
   If TabStrip1.Tabs(1).Selected Then
      If Check1.Value = 1 Then Cancel = True
   End If
End Sub

Private Sub TabStrip1_Click()
   Frame1(TabStrip1.SelectedItem.Index-1).ZOrder 0
End Sub