How to handle custom Popup Menu click events without callbacks (1 Viewer)

MarkK

bit cruncher
Local time
Today, 01:57
Joined
Mar 17, 2004
Messages
8,181
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

  • PopupEvents.zip
    56.8 KB · Views: 115

MarkK

bit cruncher
Local time
Today, 01:57
Joined
Mar 17, 2004
Messages
8,181
The database posted above references MS Office for the CommandBars, and MSCOMCTL.ocx for the Treeview.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:57
Joined
Feb 19, 2002
Messages
43,275
@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.
 

Mike Krailo

Well-known member
Local time
Today, 04:57
Joined
Mar 28, 2020
Messages
1,044
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.
 

KitaYama

Well-known member
Local time
Today, 17:57
Joined
Jan 6, 2022
Messages
1,541
@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:

MarkK

bit cruncher
Local time
Today, 01:57
Joined
Mar 17, 2004
Messages
8,181
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

Top Bottom