I have a treeview on my form and I'm trying to create a right click context menu that gives some options for a particular node. This is my first time using the CommandBar functionality.
In the form with the Treeview, there is the following subroutine:
Public Sub MyTreeview_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Long, ByVal y As Long)
'Clicking right mouse button activates subroutine
If Button = 2 Then
RightClickNodeOptions
End If
End Sub
Then in a separate module I've created a test subroutine to check the functionality:
Public Sub RightClickNodeOptions()
Dim cmdBAR As CommandBar
Set cmdBAR = CommandBars.Add(, msoBarPopup, False, True)
Dim cmdButtonAddChild As CommandBarButton
Set cmdButtonAddChild = cmdBAR.Controls.Add(msoControlButton)
Dim cmdButtonAddSibling As CommandBarButton
Set cmdButtonAddSibling = cmdBAR.Controls.Add(msoControlButton)
cmdButtonAddChild.Caption = "Add Child to Tree"
cmdButtonAddChild.OnAction = MsgBox("Child")
cmdButtonAddSibling.Caption = "Add Sibling to Tree"
cmdButtonAddSibling.OnAction = MsgBox("Sibling")
cmdBAR.ShowPopup
Set cmdBAR = Nothing
Set cmdButtonAddChild = Nothing
Set cmdButtonAddSibling = Nothing
End Sub
When I right click in the Treeview, both message boxes automatically pop up in order ("Child", "Sibling") before I've had a chance to select an option, then the CommandBar pops up with the two options. If I then click in one of the options in the commandbar, nothing happens.