What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleDisplay multi-column menus and popups
Keywordsmenu, multi-column menu, column
CategoriesSoftware Engineering, Controls
Subroutine InsertColumns determines which menu items must start new columns to ensure that a menu has no more than a certain number of items per column. It uses ModifyMenu and other menu API functions to indicate the menu items that should begin new menu columns.

The Form_Load event handler uses InsertColumns to make the State main menu use columns with no more than 10 items each.

The API menu functions do not seem to work normally on menus that are not visible. For instance, you normally hide popup menus until you need them. If you hide the popup menu the way you normally do, then when the menu reappears the column information is lost.

To work around this, the Label1_MouseDown event handler makes the popup menu (with a blank caption) visible, calls InsertColumns to make it use columns, displays it using PopupMenu, and then hides the menu again.

' Insert columns in this menu so it has at
' most items_per_column items in each column.
Private Sub InsertColumns(ByVal h_menu As Long, ByVal _
    items_per_column As Integer)
Dim num_items As Long
Dim i As Integer
Dim item_id As Long
Dim item_name As String
Dim item_length As Long

    ' See how many items are in the menu.
    num_items = GetMenuItemCount(h_menu)

    For i = items_per_column + 1 To num_items Step _
        ' Get this entry's name.
        item_name = Space$(256)
        item_length = GetMenuString(h_menu, _
            i - 1, item_name, Len(item_name), _
            item_name = Left$(item_name, item_length)

        ' Get the item's ID.
        item_id = GetMenuItemID(h_menu, i - 1)

        ' Make this entry begin a column.
        ModifyMenu h_menu, i - 1, _
            item_id, item_name
    Next i
End Sub

' Create menu entries.
Private Sub Form_Load()
Dim h_menu As Long
Dim h_submenu As Long

    ' Create the state popup and menu entries.

    ' Get the main menu handle.
    h_menu = GetMenu(Me.hwnd)

    ' -----------
    ' States menu
    ' -----------
    ' Get the State menu's handle.
    h_submenu = GetSubMenu(h_menu, 1)

    ' Make 10 entry columns in the State menu.
    InsertColumns h_submenu, 10
End Sub

Private Sub Label1_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
Dim h_menu As Long
Dim h_submenu As Long

    If Button = vbRightButton Then
        ' Before displaying the popup menu,
        ' make it visible and give it columns.
        mnuPopup.Visible = True

        ' Get the main menu handle.
        h_menu = GetMenu(Me.hwnd)
        ' ----------
        ' Popup menu
        ' ----------
        ' Get the popup menu's handle.
        h_submenu = GetSubMenu(h_menu, 2)
        ' Make 10 entry columns in the popup menu.
        InsertColumns h_submenu, 10
        ' Display the popup menu.
        PopupMenu mnuPopup

        ' Hide the menu in the menu bar again.
        mnuPopup.Visible = False
    End If
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.