How to add a context menu

A

Ammar Ismail

Dear all,

Kindly plz define through code, how to add a customized context menu which
should appear after right clicking on a form or on control in a form.

Thanks for help
 
D

Douglas J. Steele

Here's a routine copied from one of my applications:

Private Sub UpdateCommandBar( _
MemberNM As String, _
MemberNB As Integer, _
WhichControl As String _
)
' This is a "generic" routine to let us modify the pop-up menu
On Error GoTo Err_UpdateCommandBar

Dim cmdBar As CommandBar
Dim ctlNew As CommandBarButton

If Not CommandBarExists("MemberList") Then
Set cmdBar = CommandBars.Add("MemberList", _
msoBarPopup)
Set ctlNew = cmdBar.Controls.Add
ctlNew.Style = msoButtonIconAndCaption
Set ctlNew = cmdBar.Controls.Add
ctlNew.Style = msoButtonIconAndCaption
Set ctlNew = cmdBar.Controls.Add
ctlNew.Style = msoButtonIconAndCaption
End If

Set cmdBar = CommandBars("MemberList")
If WhichControl = "lstActiveMembers" Then
Set ctlNew = cmdBar.Controls(1)
With ctlNew
.FaceId = 387
.Caption = "Edit " & MemberNM
.OnAction = "=EditCurrentActive()"
End With
Set ctlNew = cmdBar.Controls(2)
With ctlNew
.FaceId = 276
.Caption = "Make " & MemberNM & " Inactive"
.OnAction = "=InactivateCurrentActive()"
End With
Set ctlNew = cmdBar.Controls(3)
With ctlNew
.FaceId = 478
.Caption = "Delete " & MemberNM
.OnAction = "=DeleteCurrentActive()"
End With
Else
Set ctlNew = CommandBars("memberlist").Controls(1)
With ctlNew
.FaceId = 387
.Caption = "Edit " & MemberNM
.OnAction = "=EditCurrentInactive()"
End With
Set ctlNew = CommandBars("memberlist").Controls(2)
With ctlNew
.FaceId = 59
.Caption = "Make " & MemberNM & " Active"
.OnAction = "=ActivateCurrentInactive()"
End With
Set ctlNew = CommandBars("memberlist").Controls(3)
With ctlNew
.FaceId = 478
.Caption = "Delete " & MemberNM
.OnAction = "=DeleteCurrentInactive()"
End With
End If

End_UpdateCommandBar:
Exit Sub

Err_UpdateCommandBar:
MsgBox Err.Number & ": " & Err.Description
Resume End_UpdateCommandBar

End Sub

In the MouseDown event of my listbox, I call this routine like:

If Not (Me!lstActiveMembers.ListIndex < 0) Then
If (Button And acRightButton) > 0 Then
intMemberNumber = lstActiveMembers.Column(0)
strMemberName = lstActiveMembers.Column(2) & " " &
lstActiveMembers.Column(1)
UpdateCommandBar strMemberName, intMemberNumber, "lstActiveMembers"
CommandBars("MemberList").ShowPopup
End If
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top