skakooli2
skakooli2

Reputation: 1

right click menu in popup form

I'm new to this great forum. I'm trying to do right click menu on listbox. currently i'm trying to implement a right click menu which will show a simple messege box.

My problem is that the list box is on a pop up form which opened up maximized. Now when i'm right clicking on the list box i see the right click menu but when i'm clicking on one of the menu options, nothing happenning (it seems that it don't go to the function as it should). i've also putted breakpoints on the function but it never tips.

It's important to mention that if i'm setting the form popup option to no the right click menu works perfectly (when i'm clicking on one of the options i see its matching messege box).

I'm running the following vba code:

This is the mouse up event handler for my list box:

Private Sub Song_List_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

' Call the SetUpContextMenu function to ensure it is setup with most current context
' Note: This really only needs to be setup once for this example since nothing is
' changed contextually here, but it could be further expanded to accomplish this
SetUpContextMenu
' See if the right mouse button was clicked
If Button = acRightButton Then
'DoCmd.CancelEvent
CommandBars("MyListControlContextMenu").ShowPopup
End If
End Sub

setting up the "SetUpContextMenu" sub:

Public Sub SetUpContextMenu()
' Note: This requires a reference to Microsoft Office Object Library
Dim combo As CommandBarControl

' Since it may have been defined in the past, it should be deleted,
' or if it has not been defined in the past, the error should be ignored

On Error Resume Next
CommandBars("MyListControlContextMenu").Delete
On Error GoTo 0

' Make this menu a popup menu
With CommandBars.Add(Name:="MyListControlContextMenu", Position:=msoBarPopup)

' Provide the user the ability to input text using the msoControlEdit type
Set combo = .Controls.Add(Type:=msoControlEdit)
combo.Caption = "Lookup Text:" ' Add a label the user will see
combo.OnAction = "=getText()" ' Add the name of a function to call

' Provide the user the ability to click a menu option to execute a function
Set combo = .Controls.Add(Type:=msoControlButton)
combo.BeginGroup = True ' Add a line to separate above group
combo.Caption = "Lookup Details" ' Add label the user will see
combo.OnAction = "=LookupDetailsFunction()" ' Add the name of a function to call

' Provide the user the ability to click a menu option to execute a function
Set combo = .Controls.Add(Type:=msoControlButton)
combo.Caption = "Delete Record" ' Add a label the user will see

combo.OnAction = "=DeleteRecordFunction()" ' Add the name of the function to call"
combo.SetFocus
combo.Visible = True
End With

End Sub

setting up the all 3 function which shows different messege boxes on click:

Public Function getText() As String

getText = CommandBars("MyListControlContextMenu").Controls(" Lookup Text:").Text

' You could optionally do something with this text here,
' such as pass it into another function ...
MsgBox "You typed the following text into the menu: " & getText

End Function

Public Function LookupDetailsFunction() As String

LookupDetailsFunction = "Hello World!"

MsgBox LookupDetailsFunction, vbInformation, "Notice!"

End Function

Public Function DeleteRecordFunction()

' If Not IsNull(Forms!MyFormName.Controls("Song_List").Colu mn(0)) Then
MsgBox "Record Deleted"
' End If

End Function

Upvotes: 0

Views: 3559

Answers (1)

Albert D. Kallal
Albert D. Kallal

Reputation: 49329

Just set the Shortcut menu bar setting in the property sheet (in the other tab) to the name of your shortcut menu.

You at that point should not need ANY code to display/launch the context menu you created.

Upvotes: 0

Related Questions