Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3063|回复: 2

关于给窗体文本框添加右键菜单的问题

[复制链接]
发表于 2016-10-17 17:31 | 显示全部楼层 |阅读模式
用一下这段代码已经可以实现给窗体textbox添加右键菜单,但存在bug,每次点选菜单就会自动的更改小键盘的NmLK状态,请高手帮忙看一下问题出在哪儿。在网上搜遍了右键菜单的代码,也就这段好比较好用,就是这个bug暂时还解决不了。

  1. Sub ShowBar()
  2.     Dim oCmdBar As CommandBar
  3.     Dim oCtrl As CommandBarControl
  4.     On Error Resume Next
  5.     CommandBars("txtBar").Delete
  6.     On Error GoTo 0
  7.     Set oCmdBar = CommandBars.Add(Name:="txtBar", Position:=msoBarPopup, Temporary:=True)
  8.     With oCmdBar
  9.         Set oCtrl = .Controls.Add(Type:=msoControlButton)
  10.         With oCtrl
  11.             .Caption = "复 制  Ctrl+C"
  12.             .OnAction = "Macro1"
  13.         End With
  14.         Set oCtrl = .Controls.Add(Type:=msoControlButton)
  15.         With oCtrl
  16.             .Caption = "剪 切  Ctrl+X"
  17.             .OnAction = "Macro2"
  18.         End With
  19.         Set oCtrl = .Controls.Add(Type:=msoControlButton)
  20.         With oCtrl
  21.             .Caption = "粘 贴  Ctrl+V"
  22.             .OnAction = "Macro3"
  23.         End With
  24.         Set oCtrl = .Controls.Add(Type:=msoControlButton)
  25.         With oCtrl
  26.             .Caption = "清 除  Del"
  27.             .OnAction = "Macro4"
  28.         End With
  29.         
  30.     End With
  31.     oCmdBar.ShowPopup
  32. End Sub

  33. Sub Macro1()
  34.     Application.SendKeys "^c", True
  35. End Sub
  36. Sub Macro2()
  37.     Application.SendKeys "^x", True
  38. End Sub
  39. Sub Macro3()
  40.     Application.SendKeys "^v", True
  41. End Sub
  42. Sub Macro4()
  43.     Application.SendKeys "{del}", True
  44. End Sub

  45. Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  46.     If Button = xlSecondaryButton Then
  47.         ShowBar
  48.         CommandBars("txtBar").Delete
  49.     End If
  50. End Sub
复制代码


excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2016-10-17 22:35 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2016-11-3 15:27 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-28 21:57 , Processed in 0.300006 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表