【发布时间】:2020-05-05 15:51:44
【问题描述】:
我创建了一个自定义右键上下文菜单
当我点击任何子菜单项时,我想更新父按钮标题
Option Explicit
Public Const Mname As String = "MyPopUpMenu"
Sub PopUpMenu()
' Create the custom right click menu.
Call RClickMenu
' Display the popup menu.
On Error Resume Next
Application.CommandBars(Mname).ShowPopup
On Error GoTo 0
End Sub
自定义右键菜单:
Sub RClickMenu()
Dim MenuItem As CommandBarPopup
Dim SectionType As String
= "Sections"
' Add the popup menu.
With Application.CommandBars.Add(Name:=Mname, Position:=msoBarPopup, _
MenuBar:=False, Temporary:=True)
Set MenuItem = .Controls.Add(Type:=msoControlPopup)
With MenuItem
.caption = "File Type"
With .Controls.Add(Type:=msoControlButton)
.caption = "File 1"
.OnAction = "setCaption"
.Parameter = "file1"
End With
With .Controls.Add(Type:=msoControlButton)
.caption = "File 2"
.OnAction = "setCaption"
.Parameter = "file2"
End With
End With
End With
End Sub
鼠标右键点击事件:
Public Sub btnFindSections_MouseDown(ByVal button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If button = 1 Then
ActiveWorkbook.FollowHyperlink "https://www.file.com"
ElseIf button = 2 Then
PopUpMenu
End If
End Sub
设置字幕方式:
Sub setCaption()
Select Case CommandBars.ActionControl.Parameter
Case "Sections"
ActiveSheet.Shapes("CommandButton1").Name = "Sections" // Error saying item not found
End Select
End Sub
在setCaption() 中,我需要找到父按钮并将其标题从Find files 更改为Find Sections
【问题讨论】:
-
我试着玩你的“PopUpMenu”,我问自己为什么在
Application.CommandBars(Mname).ShowPopup行之前使用On Error Resume Next。通常,只有当特定的CommandBar可以创建两次时,才会发生错误。在这种情况下,最好在创建之前删除它,但我很好奇你的情况是什么原因...... -
@FaneDuru 该行是从我发现的示例中复制的。我需要检查它的意义
-
试试,请在
ShowRightClickMenusub 中使用DeleteToolbar,就在With Application.CommandBars.aDD(...行之前。潜艇看起来像:Private Sub DeleteToolbar()On Error Resume NextApplication.CommandBars(Mname).DeleteOn Error GoTo 0End Sub。注意,有 5 行代码。在这里,On Error Resume Next对于不存在这样的条的情况很重要... -
@FaneDuru 知道如何在右键菜单中将控制按钮字体设置为粗体吗? stackoverflow.com/questions/61677579/…
-
是的,我想是的,但我现在没有时间......