这是我的代码。一旦我开始创建4个以上的选项卡,选项卡就会到处都是,Listbox的高度就会覆盖这些选项卡。我该如何解决这个问题?
Private Sub Command5_Click()
Static Xx As Integer
Xx = Xx + 1
Text4.Text = Xx
Dim x As Integer
Set txt = Me.Controls.Add("VB.listbox", "list3" & Text4.Text, Me)
Set txt.Container = SSTab1
For x = 0 To List2.ListCount - 1
If InStr(List2.List(x), "download") Then
SSTab1.Tabs = Text4.Text
SSTab1.Tab = 0
SSTab1.TabCaption(Text4.Text - 1) = "keywords" & " " & Text4.Text - 1
txt.Visible = True
txt.Height = "3855"
txt.Width = "7575"
txt.Left = 180
txt.Top = 480
txt.AddItem List2.List(x)
End If
Next x
End Sub
我创建了以下辅助方法来解决这个问题:
Private Sub ResizeTabControls()
Dim c As Control
Dim lb As ListBox
For Each c In Me.Controls
If TypeOf c Is ListBox Then
Set lb = c
If InStr(1, lb.Name, "list3") > 0 Then
lb.Height = TabClientHeight
lb.Top = TabTabsHeight
End If
End If
Next
End Sub
Private Function TabClientHeight() As Single
TabClientHeight = SSTab1.Height - TabTabsHeight
End Function
Private Function TabTabsHeight() As Single
Dim Number As Single
Dim TabRows As Integer
'calculate rows rounded up
Number = SSTab1.Tabs / SSTab1.TabsPerRow
If Number - Int(Number) = 0 Then
TabRows = Int(Number)
Else
TabRows = Int(Number + 1)
End If
'calculate height
TabTabsHeight = SSTab1.TabHeight * TabRows
End Function
有了这些,您现有的代码看起来是这样的:
Private Sub Command5_Click()
Static Xx As Integer
Dim x As Integer
Dim txt As ListBox
Xx = Xx + 1
Text4.Text = Xx
For x = 0 To List2.ListCount - 1
If InStr(List2.List(x), "download") Then
SSTab1.Tabs = Text4.Text
SSTab1.Tab = Text4.Text - 1 'controls get added to the active tab
SSTab1.TabCaption(Text4.Text - 1) = "keywords" & " " & Text4.Text - 1
Set txt = Me.Controls.Add("VB.ListBox", "list3" & Text4.Text, Me)
Set txt.Container = SSTab1
txt.Visible = True
'txt.IntegralHeight = False 'can only be set thru the designer
txt.Height = TabClientHeight 'exact height needs IntegralHeight = False
txt.Width = 7575
txt.Left = 180
txt.Top = TabTabsHeight
txt.AddItem List2.List(x)
End If
Next x
'resize existing controls whenever a new row of tabs is created
If SSTab1.Tabs > 1 And SSTab1.Tabs Mod SSTab1.TabsPerRow = 1 Then
ResizeTabControls
End If
End Sub