【发布时间】:2013-12-12 22:36:35
【问题描述】:
我是这个很棒的论坛的新手。 我正在尝试在列表框上做右键菜单。 目前我正在尝试实现一个右键菜单,它将显示一个简单的消息框。
我的问题是列表框在一个弹出表单上,它打开了最大化。现在,当我右键单击列表框时,我看到了右键单击菜单,但是当我单击其中一个菜单选项时,什么也没有发生(似乎它没有按应有的方式进入该功能)。我还在函数上设置了断点,但它从不提示。
值得一提的是,如果我将表单弹出选项设置为 no,则右键菜单可以完美运行(当我单击其中一个选项时,我会看到其匹配的消息框)。
我正在运行以下 vba 代码:
这是我的列表框的鼠标向上事件处理程序:
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
设置“SetUpContextMenu”子:
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
设置所有 3 个功能,在点击时显示不同的消息框:
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
【问题讨论】:
标签: ms-access vba ms-access-2010