如何实现窗体中的TEXTBOX有普通右键菜单

本文介绍如何在应用程序中创建自定义命令栏,并通过宏实现快捷键操作,包括复制、剪切、粘贴和清除功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sub ShowBar()
    Dim oCmdBar As CommandBar
    Dim oCtrl As CommandBarControl
    On Error Resume Next
    CommandBars("txtBar").Delete
    On Error GoTo 0
    Set oCmdBar = CommandBars.Add(Name:="txtBar", Position:=msoBarPopup, Temporary:=True)
    With oCmdBar
        Set oCtrl = .Controls.Add(Type:=msoControlButton)
        With oCtrl
            .Caption = "复 制  Ctrl+C"
            .OnAction = "Macro1"
        End With
        Set oCtrl = .Controls.Add(Type:=msoControlButton)
        With oCtrl
            .Caption = "剪 切  Ctrl+X"
            .OnAction = "Macro2"
        End With
        Set oCtrl = .Controls.Add(Type:=msoControlButton)
        With oCtrl
            .Caption = "粘 贴  Ctrl+V"
            .OnAction = "Macro3"
        End With
        Set oCtrl = .Controls.Add(Type:=msoControlButton)
        With oCtrl
            .Caption = "清 除  Del"
            .OnAction = "Macro4"
        End With
        
    End With
    oCmdBar.ShowPopup
End Sub

Sub Macro1()
    Application.SendKeys "^c", True
End Sub
Sub Macro2()
    Application.SendKeys "^x", True
End Sub
Sub Macro3()
    Application.SendKeys "^v", True
End Sub
Sub Macro4()
    Application.SendKeys "{del}", True
End Sub

Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If Button = xlSecondaryButton Then
        ShowBar
        CommandBars("txtBar").Delete
    End If
End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值