PDA

View Full Version : [SOLVED] UserForm Right-Click Menu



Daxton A.
07-19-2004, 12:11 PM
Say that I have created a form but I want the same options from the right menu when u click on a cell. Like Cut, Copy, Paste, etc. How is this done?

TonyJollans
07-19-2004, 12:50 PM
Hi,

You need to put code in the MouseUp event. Something like this should get you started ..


Private Sub UserForm_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = xlSecondaryButton Then
Application.CommandBars("Cell").ShowPopup
End If
End Sub


.. but I'm not sure you really want this menu on a Form.

Daxton A.
07-19-2004, 01:05 PM
well what I want mainly is to be able to Cut, Copy & Paste.

Scottie P
07-19-2004, 01:29 PM
Daxton and Tony, thank you for the question and response - it has given me another one of my wonderous (yet useless) ideas~! :rolleyes:

Regards,

Scott

Daxton A.
07-19-2004, 02:45 PM
I got an error when I tried this. It said it failed to do the .showpopup property. I typed exactly what you had listed, was "Cell" just a name for which cell to actually put in there or what?

Zack Barresse
07-19-2004, 03:22 PM
Works for me. "Cell" is not a reference to an actual cell but that specific popup menu. That's Excel's default right click menu name. In the VBE, right click your userform and paste the code in there, not on any button or object. Where did you paste it?

parry
07-19-2004, 05:02 PM
Hi, I tried this against a TextBox control instead of the UserForm object just to see if it would allow me to copy text from the box. Unfortunately, the CommandBars("Cell") is designed to work on cells only so when you select copy its actually copying the active cell/s rather than the selected text. Quite a clever idea though Tony but unfortunately doesnt work in practice.

I have seen others create context menus but only via API calls. Your post makes me wonder whether you could create a temp custom commandbar and use it as a pop-up or perhaps change the OnAction for the cells commandbar.

Ivan F Moala
07-20-2004, 01:49 AM
Hi, I tried this against a TextBox control instead of the UserForm object just to see if it would allow me to copy text from the box. Unfortunately, the CommandBars("Cell") is designed to work on cells only so when you select copy its actually copying the active cell/s rather than the selected text. Quite a clever idea though Tony but unfortunately doesnt work in practice.

I have seen others create context menus but only via API calls. Your post makes me wonder whether you could create a temp custom commandbar and use it as a pop-up or perhaps change the OnAction for the cells commandbar.

Yes you can do this.

TonyJollans
07-20-2004, 03:35 AM
Sorry I didn't get back here last night. As I said I didn't really think you wanted the Cell CommandBar on a UserForm but hoped it would give you a starting point - as Parry states, the Cell menu works on Cells!

You will almost certainly have to create your own menu to do what you want and also set it to popup on right click in each individual control you want it to work with, rather than the userform itself. I will knock up a sample and post later

Scott - do tell - what is your wondrous (yet useless) idea????

TonyJollans
07-20-2004, 09:51 AM
Well, I probably should have kept quiet on this one as it isn't entirely straightforward.

The Cut, Copy and Paste actions move data between the Clipboard and somewhere else - and it is the somewhere else which is variable and either built in or defined by you in your code. The various Excel menu options have the somewhere else pre-defined as being the current selection within the workbook. AFAIK, there is nothing within Excel which targets Userform Controls for this, but the standard Windows keyboard shortcuts of Ctrl+x, Ctrl+c and Ctrl+v (for Cut, Copy, and Paste, respectively) target whatever has the focus at the time whether it's a cell, a textbox on a form, or anything else.

Rather than trying to mimic Windows' behaviour, the simplest thing to do seems to be to use it. The attached workbook demonstrates this. It has a simple Userform with 3 textboxes and a Label. In the form's Initialization routine it creates a temporary Command Bar with Cut, Copy and Paste options copied from the Cell Command Bar, and overrides their behaviour so that they each execute a simple procedure, which Procedures simply use Sendkeys to perform the Windows actions. Right click on each textbox, the label, and the Userform itself is set up to invoke the command bar as a popup.

Without knowing exactly what you want, this seems to cover most of the angles but please post back if you want something different.

Daxton A.
07-20-2004, 10:28 AM
:vv YEA THATS WHAT I WANT TONY. THANK YOU SO MUCH!:vv
And I want to say that I have learned a few other things about VB from ur shortcuts and things like that.

Daxton A.
07-20-2004, 10:52 AM
This is probably a stupid question.

Why does the Cut command have an ampersand in the middle of itself & the others don't.

With Application.CommandBars("Cell")
Set cbc = .Controls("Cu&t").Copy(cb): cbc.OnAction = "myCut"
Set cbc = .Controls("&Copy").Copy(cb): cbc.OnAction = "myCopy"
Set cbc = .Controls("&Paste").Copy(cb): cbc.OnAction = "myPaste"
End With

TonyJollans
07-20-2004, 11:11 AM
The ampersand indicates the position of the underlined shortcut character so Cut (3rd character, t, underlined) and Copy and Paste (first characters underlined).

Daxton A.
07-20-2004, 11:17 AM
I gotcha.

thalamis
11-15-2007, 06:55 AM
Is there any way of doing this on a textbox on a worksheet, rather that on a userform ?

Bob Phillips
11-15-2007, 06:58 AM
Start a new thread, this is a tad old and jaded.

mikepfly2
12-17-2019, 11:55 AM
I know this is really old, but anyone know where to find the attachment TonyJollans is referencing in this post (#10 above)? I don't see any attachment or anywhere to download anything...
"Rather than trying to mimic Windows' behaviour, the simplest thing to do seems to be to use it. The attached workbook demonstrates this. It has a simple Userform with 3 textboxes and a Label."

SamT
12-18-2019, 09:43 AM
Attachments fall into the abyss of time after a bit.. Can't allow our severs to become full.