Cur*_*che 14
首先创建一个_MouseUp事件以在相应的控件上执行,以查看是否单击了鼠标右键,如果是,则调用该.ShowPopup方法.
当然这假定了
Private Sub MyListControlName_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Long, ByVal Y As Long)
' 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
CommandBars("MyListControlContextMenu").ShowPopup
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
由于此时Command Bar MyListControlContextMenu未定义,我在单独的模块中定义Menu,如下所示:
Public Sub SetUpContextMenu()
' Note: This requires a reference to Microsoft Office Object Library
Dim combo As CommandBarComboBox
' 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
End With
End Sub
Run Code Online (Sandbox Code Playgroud)
由于引用了三个功能,我们可以继续将这些定义如下 -
getText:注意,此选项需要引用Command Bar菜单名称的名称以及控件标题的名称.
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
Run Code Online (Sandbox Code Playgroud)
LookupDetailsFunction:对于这个例子,我将创建一个shell函数并返回文本"Hello World!".
Public Function LookupDetailsFunction() As String
LookupDetailsFunction = "Hello World!"
MsgBox LookupDetailsFunction, vbInformation, "Notice!"
End Function
Run Code Online (Sandbox Code Playgroud)
DeleteRecordFunction:对于这个例子,我将通过对null检查它来确保控件仍然有效,如果仍然有效,将执行查询以从表中删除记录.
Public Function DeleteRecordFunction() As String
If Not IsNull(Forms!MyFormName.Controls("MyListControlName").Column(0)) Then
Currentdb.Execute _
"DELETE * FROM [MyTableName] " & _
"WHERE MyKey = " & Forms!MyFormName.Controls("MyListControlName").Column(0) & ";"
MsgBox "Record Deleted", vbInformation, "Notice!"
End If
End Function
Run Code Online (Sandbox Code Playgroud)
注意:对于LookupDetailsFunction,DeleteRecordFunction和getText功能,这些必须是一个公共范围内正常工作.
最后,最后一步是测试菜单.要执行此操作,请打开表单,右键单击列表控件,然后从弹出菜单中选择一个选项.
可选地,button.FaceID可以用于指示与菜单弹出控件的每个实例相关联的已知办公室图标.
我发现Pillai Shyam在创建FaceID浏览器加载项方面的工作非常有用.