This project is read-only.

Tutorial 2, Part 2 - Create and Implement the Support_WebTabControl Class

In this part, we'll add the Support module and functions that are used to instance our new Composite Control class. Finally, we'll add the form-level code.

Contents

Step 1 - Create the Support Module

Step 2 - Add the Support Module Functions

Step 3 - Add the Form-level Code



Step 1 - Create the Support Module

  1. In the VBA Editor window, right click in the Project Explorer panel in the upper left and select Insert->Module.
  2. Rename the module Support_WebTabControl
  3. Save changes.

Top



Step 2 - Add the Support Module Functions

This step will familiarize you with the process of creating a creation function for a composite control and the essential steps that must be followed. To learn more about the Composite Control Support module creation function, review this article.

Public Function CreateWebTabControl(Tab_Control As TabControl) As cControl_WebTabControl

    Dim WebTC As cControl_WebTabControl
    Dim wb As cControl_WebButton
    
    Dim clrSelect As Long
    Dim clrHighlight As Long
    
    clrSelect = RGB(0, 0, 255)          'dark blue for selected buttons
    clrHighlight = RGB(192, 192, 255)   'light blue for highlighted buttons
    
    'create the advanced tab control
    Set WebTC = CompositeControls.CustomControls.GetOrCreate(CreateGenericReference(Tab_Control, ccTabControl), ccControl_WebTabControl)
    
    With WebTC
    
        'Create the event handler for the parent form and attach the new web tab control to it
        .AttachToEventHandler CreateEventHandler(CreateGenericReference(.Control.Parent, ccForm), False)
    
        'assign the webbutton collection to the advanced tab control
        Set .PageTabs = GetWebButtons(Tab_Control, clrSelect, clrHighlight)
                        
        'set up the web buttons to act as an option group
        CreateWebButtonOptionGroup .PageTabs
    
        'assign select / highlight colors for the web buttons
        'and link the tab control pages to the respective buttons
        For Each wb In .PageTabs
    
            wb.ParentEventHandler.Events("OnSelect").AddCallback WebTC, "ShowPage", ccFullSignature
    
        Next wb
        
    End With
    
End Function


Private Function GetWebButtons(Tab_Control As TabControl, SelectColor As Long, HighlightColor As Long) As Collection
    
    Dim pag As Page
    Dim ctl As Control
    Dim ctl2 As Control
    Dim wb As cControl_WebButton
    Dim WebButtons As Collection
    
    Set WebButtons = New Collection
    
    'iterate all pages
    For Each pag In Tab_Control.Pages
    
        'iterate all controls in each page
        For Each ctl In Tab_Control.Parent.Controls
        
            'check all command buttons
            If TypeOf ctl Is Access.CommandButton Then
            
                'if the command button tag matches the page's pageindex, then it's the page's web button
                If ctl.Tag = pag.PageIndex Then
                
                    'iterate controls looking for the associated label
                    For Each ctl2 In Tab_Control.Parent.Controls
                    
                        'if the control is a label
                        If TypeOf ctl2 Is Access.Label Then
                        
                            'and the top / left positions match, it's the associated label
                            If (ctl2.Top = ctl.Top) And (ctl2.Left = ctl.Left) Then
                            
                                Set wb = CreateWebButton(ctl, ctl2, , HighlightColor, SelectColor)
                                WebButtons.Add wb
                                
                            End If
                            
                        End If
                        
                    Next ctl2
                    
                End If
                
            End If
            
        Next ctl
        
    Next pag

    Set GetWebButtons = WebButtons

End Function



Note the following is taking place in the CreateWebTabControl() function:
  1. Default creation of objects.
  2. The WebButton objects which act as tabs for the control are returned in a collection from the function GetWebButtons()
  3. The collection is assigned to the WebTabControl's PageTabs property.
  4. The collection is established as an option group (that is, only one button may be selected at a time).
  5. The WebTabControl's ShowPage() callback is assigned to each cControl_WebButton's OnSelect event.

The GetWebButtons() function operates on the following assumptions:
  1. The CommandButton and Label controls that comprise each cControl_WebButton are located (Top and Left) in the same position and are sized (Width and Height) the same. If this is not true, the function will not create the corresponding cControl_WebButton object.
  2. The CommandButton for the corresponding Page control has the Page's PageIndex value stored in it's Tag property.

Top


Step 3 - Add the Form-Level Code

In the Startup Form, add the Form_Open() event and add the following line of code to it:
CreateWebTabControl Me.TabCtl#
Note that the # symbol indicates the number in the TabControl Name and is not meant to be typed literally.

Finally, save the project, run the Autoexec macro, and play!

Top

Now move on to the next part, Part III - Implement the cControl_EventHandler_WebTabControl Class

Last edited Feb 26, 2011 at 8:13 AM by vba_junkie, version 9

Comments

No comments yet.