What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleBuild images for a TabStrip control's tabs at run time in Visual Basic 6
DescriptionThis example shows how to build images for a TabStrip control's tabs at run time in Visual Basic 6
KeywordsTabStrip, control, tab, tab images, Visual Basic 6
CategoriesControls, Graphics
At run time, give the form TabStrip and ImageList controls. Define tabs for the TabStrip but don't give them any text. Just set their Key and TooltipText properties.

Also create a PictureBox named picHidden with Visible = False and Index = 0.

The following code shows how the program builds the tabs' images when the program starts. It first creates an array containing the colors it will use for the tabs. It then clears the ImageList control and sets its image width and height.

Next the code loops through the tabs. For each tab, it makes and sizes a PictureBox, clears the PictureBox with the appropriate color, and draws the tab's Key value onto the PictureBox. It copies the resulting picture into the ImageList control.

The program then sets the TabStrip's ImageList property to the ImageList control (you must do this after creating the images) and then loops through the tabs again setting their Image properties to the indexes of their images in the ImageList (you must do this after setting the TabStrip's ImageList property).

Private Sub Form_Load()
Const WID As Integer = 64
Const HGT As Integer = 20
Dim i As Integer
Dim colors(0 To 2) As OLE_COLOR
Dim txt As String

    ' Make a list of the colors we will use.
    colors(0) = &HC0FFFF
    colors(1) = &HC0FFC0
    colors(2) = &HFFFFC0

    ' Prepare the ImageList.
    ImageList1.ImageWidth = WID
    ImageList1.ImageHeight = HGT

    ' Build the images.
    For i = 0 To 2
        If i > 0 Then Load picHidden(i)

        picHidden(i).Picture = Nothing
        picHidden(i).Width = Me.ScaleX(WID, vbPixels, _
        picHidden(i).Height = Me.ScaleY(HGT, vbPixels, _
        picHidden(i).AutoRedraw = True
        picHidden(i).BackColor = colors(i)
        picHidden(i).ScaleMode = vbPixels

        txt = TabStrip1.Tabs(i + 1).Key
        picHidden(i).CurrentX = (WID - _
            picHidden(i).TextWidth(txt)) / 2
        picHidden(i).CurrentY = (HGT - _
            picHidden(i).TextHeight(txt)) / 2

        picHidden(i).Print txt
        picHidden(i).Picture = picHidden(i).Image
        ImageList1.ListImages.Add _
    Next i

    ' Set the TabStrip's ImageList property.
    Set TabStrip1.ImageList = ImageList1

    ' Set the tabs' Image properties.
    For i = 0 To 2
        TabStrip1.Tabs(i + 1).Image = i + 1
    Next i
End Sub
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.