Class to Build Cascading Pop-up Menus

Category:
System/API
Type:
Classes
Difficulty:
Intermediate
Author:
Walter Wong

Version Compatibility: Visual Basic 6

More information:
'   This class can build a hierarchical popup menu
'   Features:
'     o Build cascade popup menu easily
'     o Menu item can be disabled, grayed, default, checked
'     o Menu column break

Private Sub Command1_Click()
    Dim mnu As cMenu: Set mnu = New cMenu
    With mnu
        .AddItem 1, "Default", True
        .AddItem 0, "-"
        .AddItem 2, "Child Menus", , , True         ' Disabled menu item must has ID other than 0
        Dim i As Long
        For i = 1 To 10
            Dim submnu As cMenu: Set submnu = New cMenu
            With submnu
                .Caption = "Child Menu " & i
                .AddItem i * 10 + 1, "Child Menu " & i
            End With
            .AddItem i * 10, submnu
        Next
        .AddItem 0, "-"
        .AddItem 3, "Checked", , True
        .AddItem 4, "Grayed", , , , True
        .AddItem 5, "New Column", , , , , True
        
        MsgBox .TrackPopup(, , , Command1.hwnd)
    End With
End Sub

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: