Advertisement
ba5tz

Context Menu VBA

Jul 27th, 2019
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'di Modul
  2. Option Explicit
  3. Public g_txtActiveTextbox As MSForms.TextBox
  4.  
  5. Public Sub ShowDemo()
  6.     UserForm1.Show
  7. End Sub
  8.  
  9. Public Sub Textbox_Clear()
  10.     g_txtActiveTextbox.Text = ""
  11. End Sub
  12. Public Sub Textbox_Select()
  13.     g_txtActiveTextbox.SelStart = 0
  14.     g_txtActiveTextbox.SelLength = Len(g_txtActiveTextbox.Text)
  15. End Sub
  16. Public Sub Textbox_Paste()
  17.     g_txtActiveTextbox.Paste
  18. End Sub
  19. Public Sub Textbox_Copy()
  20.     g_txtActiveTextbox.Copy
  21. End Sub
  22. Public Sub Textbox_Cut()
  23.     g_txtActiveTextbox.Cut
  24. End Sub
  25.  
  26. 'di userform
  27. Sub BuildTextboxMenu(X As Single, Y As Single)
  28.     'Hapus Menu existing
  29.    On Error Resume Next
  30.     'CommandBars("MyTextboxMenu").Delete
  31.    
  32.     With CommandBars.Add(Name:="MyTextboxMenu", Position:=msoBarPopup)
  33.         With .Controls.Add(Type:=msoControlButton)
  34.             .OnAction = "Textbox_Cut"
  35.             .Caption = "Cu&t"
  36.         End With
  37.         With .Controls.Add(Type:=msoControlButton)
  38.             .OnAction = "Textbox_Copy"
  39.             .Caption = "&Copy"
  40.         End With
  41.         With .Controls.Add(Type:=msoControlButton)
  42.             .OnAction = "Textbox_Paste"
  43.             .Caption = "&Paste"
  44.         End With
  45.         With .Controls.Add(Type:=msoControlButton)
  46.             .OnAction = "Textbox_Clear"
  47.             .Caption = "Cle&ar"
  48.         End With
  49.         With .Controls.Add(Type:=msoControlButton)
  50.             .OnAction = "Textbox_Select"
  51.             .Caption = "Select A&ll"
  52.             .BeginGroup = True
  53.         End With
  54.         .ShowPopup
  55.     End With
  56.     CommandBars("MyTextboxMenu").Delete
  57. End Sub
  58.  
  59. 'penggunaan
  60. Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  61. If Button = xlSecondaryButton Then
  62.     Set g_txtActiveTextbox = TextBox1
  63.     BuildTextboxMenu X, Y
  64. End If
  65. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement