PDA

View Full Version : [SOLVED] Right Click TextBox Menu



sheeeng
06-30-2005, 12:17 AM
Hi all, :hi:

Can VBA Userform support Right Click Menu of TextBox?
Just wish to had some respones, since I don't know whether it is supported in VBA.

eg.

1. create TextBox1
2. run Form
3. right click TextBox1
4. option Copy and Paste appear with their intended functions.

Thx in advance. :friends:

Bob Phillips
06-30-2005, 01:02 AM
Hi all, :hi:

Can VBA Userform support Right Click Menu of TextBox?
Just wish to had some respones, since I don't know whether it is supported in VBA.

eg.

1. create TextBox1
2. run Form
3. right click TextBox1
4. option Copy and Paste appear with their intended functions.

Thx in advance. :friends:

A short cut menu can be created as a popup commandbar. The menu can be activated from the MouseUp event of the textbox.


You can create a popup menu using code like the following in a standard module.



Sub MakePopUp()
'Remove any old instance of MyPopUp
On Error Resume Next
CommandBars("MyPopUp").Delete
On Error GoTo 0
With CommandBars.Add(Name:="MyPopUp", Position:=msoBarPopup)
.Controls.Add Type:=msoControlButton, ID:=19
.Controls.Add Type:=msoControlButton, ID:=22
End With
End Sub


In your userform code module choose the Textbox from the top left dropdown and the MouseUp event from the top right dropdown and insert something like the following code:



Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 2 Then
Application.CommandBars("MyPopUp").ShowPopup
End If
End Sub


But Ctrl-C and Ctrl-V work fine in a textbox?

sheeeng
06-30-2005, 08:02 PM
But Ctrl-C and Ctrl-V work fine in a textbox?

Yes, Ctrl-C and Ctrl-V works...
But this is just an example.....
I might need to run certain macro form the menu....

Thx....:friends:

sheeeng
06-30-2005, 08:05 PM
Application.CommandBars("MyPopUp").ShowPopup



This line has error...

Please help....

Thx. :friends:

Justinlabenne
06-30-2005, 08:33 PM
You need to create the popup first:


Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
MakePopUp
If Button = 2 Then
Application.CommandBars("MyPopUp").ShowPopup
End If
End Sub

sheeeng
06-30-2005, 08:47 PM
You need to create the popup first:


Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
MakePopUp
If Button = 2 Then
Application.CommandBars("MyPopUp").ShowPopup
End If
End Sub


Sorry. I don't know how to create...:doh:

Justinlabenne
06-30-2005, 08:51 PM
Assuming you have a textbox named "Textbox1" in your userform, paste the code in the userform code module that I jsut posted. It replace the once xld gave you, it makes sure the bar is created on the right-click.

Notice how it says, "MakePopUp" above the if button = 2 line, that creates it.
:thumb

sheeeng
06-30-2005, 08:51 PM
[QUOTE=xld]


Sub MakePopUp()
'Remove any old instance of MyPopUp
On Error Resume Next
CommandBars("MyPopUp").Delete
On Error GoTo 0
With CommandBars.Add(Name:="MyPopUp", Position:=msoBarPopup)
.Controls.Add Type:=msoControlButton, ID:=19
.Controls.Add Type:=msoControlButton, ID:=22
End With
End Sub

QUOTE]

You mean I must make popup by running these sub first above all?

Can we assign the menu to a sub or function?

eg. Show WorkBook Path below....



Sub ShowBookPath()
Msgbox Application.Path
End Sub

sheeeng
06-30-2005, 08:52 PM
With CommandBars.Add(Name:="MyPopUp", Position:=msoBarPopup)
.Controls.Add Type:=msoControlButton, ID:=19
.Controls.Add Type:=msoControlButton, ID:=22
End With



Where can I know id=19 is copy or others?
Any good references on this?

Justinlabenne
06-30-2005, 08:56 PM
Right click the textbox on the form


for the id's
http://puremis.net/excel/code/016.shtml

sheeeng
07-03-2005, 08:27 AM
Great resources....:thumb
Another solved.. :friends:

Bob Phillips
07-03-2005, 08:58 AM
You need to create the popup first:


Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
MakePopUp
If Button = 2 Then
Application.CommandBars("MyPopUp").ShowPopup
End If
End Sub


I wouldn't run it there, far too inefficient. In the workbook_open or at worst, userform initialize.

Justinlabenne
07-03-2005, 11:57 AM
Yeah noticed that, and thanks for editing your post xld, first it said "efficient", I was wondering what you meant, :think:

I would probably go from the initialize event....

Bob Phillips
07-03-2005, 01:42 PM
Yeah noticed that, and thanks for editing your post xld, first it said "efficient", I was wondering what you meant,.

Too much Sunday evening vino ...

sheeeng
07-03-2005, 06:41 PM
Thanks for all your help.