Vb6 changing colors of SSTab at runtime using label in the background

vb6

I am trying to update an old VB6 application. I am struggling with changing the backcolor of the SSTab control at runtime.

I know that I cannot just set the Backcolor to the required colour.

At designtime I have created a label on each tab and then resized the label to the sstab dimensions. Then I set the label backcolor. This works fine.

However, what I really want to do is to do this at runtime so that if a new tab control is added on a form in the future I dont need to manually do this at designtime each time.

I would be grateful if someone can point me in the right direction to create and display a label on every tab of a SSTab control at runtime?

I dont have frames on all the tabs so I dont want to use the frame container to hold the label. I would rather just have the label placed on the tab container at runtime.

Thanks and I look forward to your replies.

Best Answer

As svinto says, you can load new controls to a Control Array, but there's a bit more to it than that.

Firstly the new controls are not Visible, so you must set .Visible=True You also need to set the current tab before creating the control to get it sited on that tab.

Assuming you already have the first label on the first tab:

For i = 1 To Me.SSTab1.Tabs - 1
    Me.SSTab1.Tab = i
    Load lblOne(i)
    lblOne(i).Visible = True
Next

Also, this doesn't work in the Form_Load event, but does in Form_Activate

There is another method to create the Labels from scratch rather than using a Control Array, which might be better as you don't need to add the first Label to the form:

Private Sub Form_Activate()
    Dim lbl() As Label
    Dim i As Integer
    Dim name As String

   ReDim lbl(SSTab1.Tabs - 1)
   For i = 0 To Me.SSTab1.Tabs - 1
        Me.SSTab1.Tab = i
        name = "MyLabel" & CStr(i)
        Set lbl(i) = Form1.Controls.Add("VB.Label", name)
        Set lbl(i).Container = Me.SSTab1
        lbl(i).Visible = True
        lbl(i).Caption = name
        lbl(i).Move 40, 345
        lbl(i).BackColor = vbGreen
    Next
End Sub
Related Topic