仅在特定的窗体控件(文本框)上启用快捷菜单 [英] Enabling Shortcut Menu only on a specific Form control (TextBox)

查看:75
本文介绍了仅在特定的窗体控件(文本框)上启用快捷菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与

This is closely related to this question, which hasn't got an answer posted.

我有一个希望在其中单击快捷菜单(右键单击弹出的菜单)的窗体,仅在该窗体上的文本框内单击时(其他所有位置均禁用).问题在于,该选项似乎仅对整个表单是全局的.

I have a Form where I would like to have the Shortcut Menu (the one that pops up with right click) enabled only when clicked inside a TextBox on that Form (disabled every where else). The problem is that it seems like that option is only global to the entire Form.

将窗体的ShortcutMenu设置为False,我可以使用"ShortcutMenu = True"在TextBox的"On Enter"事件中将其设置为True,使右键单击不执行任何操作,直到输入TextBox.这样做的问题是,当用户输入TextBox时,如果他右键单击表单中的其他任何位置,都会出现右键单击菜单,这不是我想要的.

Having the Form's ShortcutMenu set to False, I can set it to True in a TextBox's "On Enter" event using "ShortcutMenu = True", making the right click do nothing until the TextBox is entered. The problem with this is that when a user enters the TextBox, if he right clicks anywhere else in the form, the right click menu will appear, which is not as I intend.

在同一TextBox的"On Exit"事件中通过使用"ShortcutMenu = False"来禁用ShortcutMenu是有效的,但前提是用户单击另一个控件(而不是之前),这不能解决问题.

Disabling the ShortcutMenu by using "ShortcutMenu = False" in the "On Exit" event of the same TextBox works but only if the user clicks on another control, not before, which doesn't solve the problem.

在某个地方执行右键单击操作之前,是否仍要运行"ShortcutMenu = False"? (输入文本框后).像鼠标移开此控件"事件一样?

Is there anyway to run "ShortcutMenu = False" before the right click action on someplace is executed? (After entering the TextBox). Something like an "the mouse moved away from this control" event?

推荐答案

您可以通过动态创建2个自定义的快捷菜单来实现此目的.一个基本上是整个表单的虚拟快捷菜单,其中没有控件,另一个将是您希望出现在实际控件上的菜单.我将这些设置为临时的.您需要将自定义控件调整为想要在右键单击时显示的内容.

you can accomplish this by creating 2 customized shortcut menus on the fly. one will basically be a dummy shortcut menu for the entire form, with no controls in it, the other will be the one you want to appear on your actual control. I have these set to temporary. you will need to adjust the custom controls to what you want displayed when they right-click.

Private Const CntrlShortcutMenu As String = "CntrlShortCutMenu"
Private Const FormShortcutMenu As String = "FormShortCutMenu"

Private Sub Form_Load()
 Call CreateControlShortcutMenu("txtvalue1")
 Call CreateDummyShortcutMenuonForm
End Sub

Private Sub Form_Unload(Cancel As Integer)
 DeleteShortCutMenu (CntrlShortcutMenu)
 DeleteShortCutMenu (FormShortcutMenu)
End Sub

Private Sub CreateDummyShortcutMenuonForm()
    Call DeleteShortCutMenu(FormShortcutMenu)
    Call CommandBars.Add(FormShortcutMenu, 5, False, True)
    Me.ShortcutMenuBar = FormShortcutMenu
End Sub

Private Sub DeleteShortCutMenu(MenuName As String)
  On Error Resume Next
  CommandBars(MenuName).Delete
End Sub

Private Sub CreateControlShortcutMenu(CntrlName As String)

On Error GoTo errhandler

    Dim cmdShortcutMenu As Object 'Office.CommandBar

    Call DeleteShortCutMenu(CntrlShortcutMenu)

    Set cmdShortcutMenu = CommandBars.Add(CntrlShortcutMenu, 5, False, True)

        With cmdShortcutMenu

             With .Controls.Add(Type:=10)
                .Caption = "Text Editing"
                .Controls.Add Type:=1, Id:=19   'Copy
                .Controls.Add Type:=1, Id:=22   'Paste
                .Controls.Add Type:=1, Id:=2941 'Select All
             End With

             With .Controls.Add(Type:=10)
                .Caption = "Filter"
                .Controls.Add Type:=1, Id:=210  'Sort Ascending
                .Controls.Add Type:=1, Id:=211  'Sort Descending
                .Controls.Add Type:=1, Id:=640  'Filter By Selection
                .Controls.Add Type:=1, Id:=3017 'Filter Excluding Selection
                .Controls.Add Type:=2, Id:=2863 'Filter For
                .Controls.Add Type:=1, Id:=605  'Remove Filter/Sort
             End With

        End With

        Me.Controls(CntrlName).ShortcutMenuBar = CntrlShortcutMenu

ExitSub:
    Set cmdShortcutMenu = Nothing
    Exit Sub
errhandler:
    Debug.Print "CreateControlShortcutMenu", Err.Description
    Resume ExitSub
End Sub

这篇关于仅在特定的窗体控件(文本框)上启用快捷菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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