Skip to content

TabStrip Control (VBCCRTabStrip)

VBCCRTabStrip 控件提供了一个选项卡容器,允许用户通过点击标签页来切换不同的内容面板。它通常用于组织和显示相关但不同的内容组。

属性

关键属性

  • Tabs: 选项卡集合
  • SelectedItem: 当前选中的选项卡
  • TabIndex: 当前选中的选项卡索引
  • MultiRow: 是否允许多行显示
  • Style: 选项卡样式
  • TabFixedWidth: 固定宽度
  • TabMinWidth: 最小宽度
  • Alignment: 选项卡对齐方式
  • TabOrientation: 选项卡方向(顶部/底部)
  • HotTracking: 鼠标悬停效果
  • TabEnabled: 选项卡启用状态
  • TabVisible: 选项卡可见状态
  • ImageList: 图标列表

选项卡样式常量 (TbsStyleConstants)

用于 Style 属性:

  • TbsStyleTabs (0) - 标准选项卡样式
  • TbsStyleButtons (1) - 按钮样式
  • TbsStyleFlatButtons (2) - 平面按钮样式

方法

主要方法

  • AddTab(Caption As String, [Key As String], [Image As Variant], [ToolTipText As String]): 添加新选项卡
  • RemoveTab(Index As Variant): 移除选项卡
  • Clear(): 清除所有选项卡
  • GetTab(Index As Variant) As Tab: 获取选项卡对象
  • SelectTab(Index As Variant): 选择指定选项卡
  • HitTest(X As Single, Y As Single) As Long: 测试点击位置

事件

  • Click(): 点击事件
  • BeforeClick(Cancel As Boolean): 点击前事件
  • MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  • MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  • MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  • TabClick(Tab As Tab): 选项卡点击事件
  • KeyDown(KeyCode As Integer, Shift As Integer)
  • KeyPress(KeyAscii As Integer)
  • KeyUp(KeyCode As Integer, Shift As Integer)

代码示例

基本用法

vb
Private Sub Form_Load()
    ' 添加选项卡
    With TabStrip1
        .AddTab "常规"
        .AddTab "高级"
        .AddTab "设置"
        .MultiRow = False
        .Style = TbsStyleTabs
        .TabIndex = 0
    End With

    ' 显示对应面板
    ShowPanel TabStrip1.TabIndex
End Sub

Private Sub TabStrip1_Click()
    ShowPanel TabStrip1.TabIndex
End Sub

Private Sub ShowPanel(ByVal Index As Long)
    ' 隐藏所有面板
    fraGeneral.Visible = False
    fraAdvanced.Visible = False
    fraSettings.Visible = False

    ' 显示选中的面板
    Select Case Index
        Case 0: fraGeneral.Visible = True
        Case 1: fraAdvanced.Visible = True
        Case 2: fraSettings.Visible = True
    End Select
End Sub

动态选项卡

vb
Private Sub CreateDynamicTabs()
    ' 清除现有选项卡
    TabStrip1.Clear

    ' 动态添加选项卡
    For i = 1 To 5
        With TabStrip1
            .AddTab "选项卡 " & i, "TAB" & i
            .GetTab(i - 1).ToolTipText = "这是选项卡 " & i
            .GetTab(i - 1).Enabled = True
            .GetTab(i - 1).Tag = "DATA" & i
        End With
    Next i
End Sub

Private Sub TabStrip1_TabClick(Tab As Tab)
    ' 处理选项卡点击
    Debug.Print "点击了选项卡: " & Tab.Caption
    Debug.Print "选项卡数据: " & Tab.Tag
End Sub

使用图标

vb
Private Sub SetupTabsWithIcons()
    ' 设置图标列表
    Set TabStrip1.ImageList = ImageList1

    ' 添加带图标的选项卡
    With TabStrip1
        .AddTab "文档", , 0  ' 使用ImageList中索引为0的图标
        .AddTab "图片", , 1  ' 使用ImageList中索引为1的图标
        .AddTab "音频", , 2  ' 使用ImageList中索引为2的图标
        .AddTab "视频", , 3  ' 使用ImageList中索引为3的图标
    End With
End Sub

常见用例

文档界面

vb
Private Type DocumentInfo
    Caption As String
    Content As String
    Modified As Boolean
End Type

Private Documents() As DocumentInfo
Private DocumentCount As Long

Private Sub AddNewDocument()
    DocumentCount = DocumentCount + 1
    ReDim Preserve Documents(1 To DocumentCount)

    With Documents(DocumentCount)
        .Caption = "文档 " & DocumentCount
        .Content = ""
        .Modified = False
    End With

    ' 添加新选项卡
    TabStrip1.AddTab Documents(DocumentCount).Caption, _
                     "DOC" & DocumentCount

    ' 选择新文档
    TabStrip1.TabIndex = DocumentCount - 1
    txtContent.Text = ""
End Sub

Private Sub TabStrip1_Click()
    ' 保存当前文档内容
    If TabStrip1.TabIndex >= 0 Then
        Documents(TabStrip1.TabIndex + 1).Content = txtContent.Text
    End If

    ' 加载选中文档的内容
    txtContent.Text = Documents(TabStrip1.TabIndex + 1).Content
End Sub

设置页面

vb
Private Sub CreateSettingsPages()
    ' 添加设置页面
    With TabStrip1
        .AddTab "常规设置"
        .AddTab "显示"
        .AddTab "性能"
        .AddTab "网络"
        .AddTab "安全"
        .MultiRow = True
        .Style = TbsStyleButtons
    End With

    ' 初始化所有设置页面
    InitSettingsPages
End Sub

Private Sub InitSettingsPages()
    ' 创建所有设置页面的控件
    For i = 0 To TabStrip1.Tabs.Count - 1
        Dim fra As Frame
        Set fra = Controls.Add("VB.Frame", "fra" & i)

        With fra
            .Left = TabStrip1.Left
            .Top = TabStrip1.Top + TabStrip1.Height
            .Width = TabStrip1.Width
            .Height = 3000
            .Visible = (i = 0)
            .Caption = TabStrip1.Tabs(i).Caption
        End With
    Next i
End Sub

最佳实践

  1. 选项卡管理
vb
Private Sub ManageTabStates()
    With TabStrip1
        ' 更新选项卡状态
        For i = 0 To .Tabs.Count - 1
            .GetTab(i).Enabled = IsTabAccessible(i)
        Next i
    End With
End Sub
  1. 内存优化
vb
Private Sub CleanupOldTabs()
    ' 限制选项卡数量
    Const MAX_TABS As Long = 10

    With TabStrip1
        While .Tabs.Count > MAX_TABS
            .RemoveTab 0  ' 移除最旧的选项卡
        Wend
    End With
End Sub

其他提示

  • 使用 ImageList 为选项卡添加图标
  • 实现 BeforeClick 事件来取消选项卡切换
  • 使用 Tag 属性存储选项卡相关的自定义数据
  • 在 TabClick 事件中更新界面状态
  • 使用 ToolTipText 属性提供上下文帮助
  • 考虑使用 MultiRow 来处理大量选项卡
  • 在 TabOrientation 属性中控制选项卡位置
  • 定期清理未使用的选项卡以优化内存

twinBASIC及其LOGO版权为作者"韦恩"所有