How to handle custom Popup Menu click events without callbacks

MarkK

bit cruncher
Local time
Yesterday, 22:40
Joined
Mar 17, 2004
Messages
8,364
Typically in the past, when creating custom right-click popup menus, I've used the CommandBarButton.OnAction property to define the name of a callback for that button to run. Recently, however, I noticed that a CommandBarButton raises a click event.

This database demonstrates a pattern you can use to open a shortcut popup menu in an object, and handle that menu's button clicks in that same object, without callbacks.

Check it out...
 

Attachments

The database posted above references MS Office for the CommandBars, and MSCOMCTL.ocx for the Treeview.
 
@MarkK I don't have time to look at this now, but you might want to post it in Database Samples. Someone will need to approve it so notify Jon.
 
Very interesting Mark. Just took a look at it. Just need to apply it to something more tangibly useable to fully grasp what is going on.
 
@MarkK Thanks for the database.
I have a database that I think your classes fit for what I'm trying to achieve.
For now, because I hate On Error Resume Next I changed your Delete to :
Code:
    Dim CB As CommandBar
   
    For Each CB In Application.CommandBars
        If CB.Name = PN Then
            CommandBars(PN).Delete
            Exit Sub
        End If
    Next CB

I need the items in the context menu bar to open a second level menubar. I'll try to use the class to repeat the second level buttons and will be back for more questions if I hit a wall.

Thanks again.
 
Last edited:
because I hate On Error Resume Next I changed your Delete to
It's funny how we are. I see your code, and I immediately think it should be....
Code:
    Dim CB As CommandBar
    
    For Each CB In Application.CommandBars
        If CB.Name = PN Then
            CommandBars(PN).Delete
            Exit For
        End If
    Next CB
I don't mind 'On Error Resume Next' as much as I dislike 'Exit Sub.' We are whimsical...

I was trying to keep the example Db as simple as possible, but to add a sub-menu you can add this method to cPopup...
Code:
Public Function AddMenu(Caption As String, BeginGroup As Boolean, ParamArray vOptions()) As Office.CommandBarPopup
    Dim cbc As Office.CommandBarButton
    Dim var
    
    With Me.Popup.Controls.Add(msoControlPopup) ' add sub menu
        .Caption = Caption
        .BeginGroup = BeginGroup
        For Each var In vOptions                ' for each item, add an event button
            Set cbc = .Controls.Add(msoControlButton)
            cbc.Caption = CStr(var)
            
            With New cPopupButton
                btns_.Add .Load(Me, cbc), cbc.Caption
            End With
        Next
    End With
    
End Function
 

Users who are viewing this thread

Back
Top Bottom