如何将菜单项添加到默认的右键单击上下文菜单 [英] How to add a menu item to the default right click context menu

查看:85
本文介绍了如何将菜单项添加到默认的右键单击上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是创建可以与MS Access窗体上的某些控件一起使用的菜单,并能够右键单击该控件(例如,在列表框和带有选项的相关上下文特定菜单弹出窗口)上,如果单击,将触发预定义的子例程或函数.

The goal is to create menus which can be utilized with certain controls on an MS Access form and to be able to right click on a that control, for example on a listbox and a relevant context specific menu popup with options, which if clicked, would trigger a predefined subroutine or function.

以编程方式完成此任务的最佳方法是什么?

What is the best method to accomplish this programmatically?

我正在使用MS Access 2003,并且希望使用VBA进行此操作.

I am using MS Access 2003 and would like to do this using VBA.

推荐答案

首先创建一个_MouseUp事件,在相应控件上执行,以查看是否单击了鼠标右键,如果是,则调用.ShowPopup方法

First create an _MouseUp event to execute on the respective control looking to see if the right mouse button was clicked and if so, call the .ShowPopup method.

当然,这是假定

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

由于此时命令栏MyListControlContextMenu尚未定义,所以我在单独的模块中定义菜单,如下所示:

Since at this point the Command Bar MyListControlContextMenu is undefined, I define the Menu in a separate module as follows:

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 

由于已引用了三个函数,因此我们可以继续如下定义它们-

Since three function have been referenced, we can move on to define these as follows-

getText :请注意,此选项需要同时引用命令栏菜单名称的名称和控件标题的名称.

getText: Note, this option requires a reference to both the name of the Command Bar menu name as well as the name of the control caption.

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

LookupDetailsFunction :对于此示例,我将创建一个shell函数并返回文本"Hello World!".

LookupDetailsFunction: For this example, I will create a shell function and return the text "Hello World!".

Public Function LookupDetailsFunction() As String

   LookupDetailsFunction = "Hello World!"

   MsgBox LookupDetailsFunction, vbInformation, "Notice!"

End Function

DeleteRecordFunction :对于此示例,我将通过对照null来检查控件,以确保该控件仍然有效,并且如果仍然有效,将执行查询以从表中删除记录.

DeleteRecordFunction: For this example, I will ensure the control is still valid by checking it against null, and if still valid, will execute a query to remove the record from a table.

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

注意:对于LookupDetailsFunctionDeleteRecordFunctiongetText函数,它们必须在公共范围内才能正常工作.

Note: For LookupDetailsFunction, DeleteRecordFunction and getText functions, these must be within a public scope to work correctly.

最后,最后一步是测试菜单.为此,请打开表单,右键单击列表控件,然后从弹出菜单中选择一个选项.

Finally, the last step is to test the menu. To do this, open the form, right click on the list control and select one of the options from the popup menu.

(可选)button.FaceID可用于指示与菜单弹出控件的每个实例相关联的已知Office图标.

Optionally button.FaceID can be utilized to indicate a known office icon to associate with each instance of the menu popup control.

我发现 Pillai Shyam的工作在创建FaceID浏览器加载项方面非常有帮助.

I found Pillai Shyam's work on creating a FaceID Browser Add-In to be very helpful.

参考: Microsoft FaceID

这篇关于如何将菜单项添加到默认的右键单击上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆