很复杂……
VBA中的控件没有提供右键快捷菜单,用户可以使用Excel 中的命令栏自已添加右键快捷菜单。
步骤1:按<Alt+F11>组合键进入VBE窗口,单击菜单“插入”→“模块”,在其代码窗口输入以下代码:
#001 Private ActiveTB As MSForms.TextBox
#002 Public Sub CreateShortCutMenu()
#003 Dim ShortCutMenu As CommandBar
#004 Dim ShortCutMenuItem As CommandBarButton
#005 Dim sCaption As Variant
#006 Dim iFaceId As Variant
#007 Dim sAction As Variant
#008 Dim i As Integer
#009 sCaption = Array("剪切(&C)", "复制(&T)", "贴粘(&P)", "删除(&D)")
#010 iFaceId = Array(21, 19, 22, 1786)
#011 sAction = Array("Action_Cut", "Action_Copy", "Action_Paste", "Action_Delete")
#012 On Error Resume Next
#013 Application.CommandBars("ShortCut").Delete
#014 Set ShortCutMenu = Application.CommandBars.Add("ShortCut", msoBarPopup)
#015 With ShortCutMenu
#016 For i = 0 To 3
#017 Set ShortCutMenuItem = .Controls.Add(msoControlButton)
#018 With ShortCutMenuItem
#019 .Caption = sCaption(i)
#020 .faceID = Val(iFaceId(i))
#021 .OnAction = sAction(i)
#022 End With
#023 Next
#024 End With
#025 End Sub
代码解析:
第1行代码,在模块级别中声明变量ActiveTB是用来对应窗体中的文本框所触发的所有事件的变量。
CreateShortCutMenu过程用来创建标题为“ShortCut”的右键快捷菜单,并添加4个菜单项。关于自定义右键快捷菜单请参阅技巧86 。
#001 Public Sub ShowPopupMenu(txtCtr As MSForms.TextBox)
#002 Dim Action As Variant
#003 Set ActiveTB = txtCtr
#004 With Application.CommandBars("ShortCut")
#005 .Controls(1).Enabled = txtCtr.SelLength > 0
#006 .Controls(2).Enabled = .Controls(1).Enabled
#007 .Controls(3).Enabled = txtCtr.CanPaste
#008 .Controls(4).Enabled = .Controls(1).Enabled
#009 .ShowPopup
#010 End With
#011 End Sub
代码解析:
ShowPopupMenu过程根据文本框中字符的选中状态设置右键快捷菜单菜单项的Enabled属性后使用ShowPopup方法显示右键快捷菜单。
第5行代码,如果当前文本框中已有选中的字符则“剪切”按钮有效。
第6行代码,如果当前文本框中已有选中的字符则“复制”按钮有效。
第7行代码,如果剪贴板中包含对象支持的数据。则“贴粘”按钮有效。
第8行代码,如果当前文本框中已有选中的字符则“删除”按钮有效。
第9行代码,显示快捷菜单。
#001 Public Sub Action_Cut()
#002 ActiveTB.Cut
#003 End Sub
#004 Public Sub Action_Copy()
#005 ActiveTB.Copy
#006 End Sub
#007 Public Sub Action_Paste()
#008 ActiveTB.Paste
#009 End Sub
#010 Public Sub Action_Delete()
#011 Dim s As String
#012 With ActiveTB
#013 s = .SelText
#014 .Value = Replace(.Value, s, "")
#015 End With
#016 End Sub
代码解析:
Action_Cut过程是快捷菜单中单击“剪切”菜单项所运行的过程。使用Cut 方法将当前选中的文本框中的文本删除并移至剪贴板。
Action_Copy过程是快捷菜单中单击“复制”菜单项所运行的过程。使用Copy方法将文本框选中的文本复制到剪贴板上。
Action_Paste过程是快捷菜单中单击“贴粘”菜单项所运行的过程。使用Paste方法把剪贴板上的内容传送到一个文本框中。
Action_Delete过程是快捷菜单中单击“贴粘”菜单项所运行的过程。使用Replace函数将文本框中选中的文本的文本替换成空字符。
#001 Public Sub DeleteShortCutMenu()
#002 On Error Resume Next
#003 Application.CommandBars("ShortCut").Delete
#004 End Sub
代码解析:
DeleteShortCutMenu过程删除创建的右键快捷菜单。
步骤2:在VBE窗口中,单击菜单“插入”→“用户窗体”,在窗体上添加两个文本框控件。双击窗体,在其代码窗口中输入下面的代码。
#001 Private Sub UserForm_Initialize()
#002 Call CreateShortCutMenu
#003 End Sub
#004 Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
#005 If Button = 2 Then ShowPopupMenu ActiveControl
#006 End Sub
#007 Private Sub TextBox2_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
#008 If Button = 2 Then ShowPopupMenu ActiveControl
#009 End Sub
#010 Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
#011 Call DeleteShortCutMenu
#012 End Sub
代码解析:
第1行到第3行代码,窗体的Initialize事件,在窗体初始化时运行CreateShortCutMenu过程创建右键快捷菜单。
第4行到第9行代码,文本框的MouseUp事件,当用户右健单击文本框时运行ShowPopupMenu过程在选中的菜单项上显示右键快捷菜单。
第10行到第12行代码,窗体的QueryClose事件,在关闭窗体时运行DeleteShortCutMenu过程删除右键快捷菜单。
窗体运行后,右键单击文本框显示右键快捷菜单,如图 99-1所示。
参考资料:Excel_VBA常用技巧_第08章[1].控件与用户窗体.doc