Consulting

Results 1 to 18 of 18

Thread: UserForm Right-Click Menu

  1. #1
    VBAX Contributor Daxton A.'s Avatar
    Joined
    Jun 2004
    Location
    Biloxi, Mississippi
    Posts
    143
    Location

    UserForm Right-Click Menu

    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?

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi,

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

    [VBA]
    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
    [/VBA]

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

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    VBAX Contributor Daxton A.'s Avatar
    Joined
    Jun 2004
    Location
    Biloxi, Mississippi
    Posts
    143
    Location

    .....

    well what I want mainly is to be able to Cut, Copy & Paste.

  4. #4
    Just A Dude VBAX Tutor Scottie P's Avatar
    Joined
    May 2004
    Location
    Remote from 18901 USA
    Posts
    263
    Location

    Sorry to intrude...

    Daxton and Tony, thank you for the question and response - it has given me another one of my wonderous (yet useless) ideas~!

    Regards,

    Scott
    Life is Visual: Presence is Perception...
    How we see the world is how we respond to it. ~* Peace *~

  5. #5
    VBAX Contributor Daxton A.'s Avatar
    Joined
    Jun 2004
    Location
    Biloxi, Mississippi
    Posts
    143
    Location

    ....

    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?

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    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?

  7. #7
    VBAX Regular
    Joined
    Jul 2004
    Location
    Wellington, New Zealand
    Posts
    37
    Location
    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.

  8. #8
    VBAX Contributor Ivan F Moala's Avatar
    Joined
    May 2004
    Location
    Auckland New Zealand
    Posts
    185
    Location
    Quote Originally Posted by parry
    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.
    Kind Regards,
    Ivan F Moala From the City of Sails

  9. #9
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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????
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #10
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  11. #11
    VBAX Contributor Daxton A.'s Avatar
    Joined
    Jun 2004
    Location
    Biloxi, Mississippi
    Posts
    143
    Location

    Smile THANK YOU SO MUCH!

    YEA THATS WHAT I WANT TONY. THANK YOU SO MUCH!
    And I want to say that I have learned a few other things about VB from ur shortcuts and things like that.

  12. #12
    VBAX Contributor Daxton A.'s Avatar
    Joined
    Jun 2004
    Location
    Biloxi, Mississippi
    Posts
    143
    Location

    Question

    This is probably a stupid question.

    Why does the Cut command have an ampersand in the middle of itself & the others don't.
    [VBA]
    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
    [/VBA]

  13. #13
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    The ampersand indicates the position of the underlined shortcut character so Cut (3rd character, t, underlined) and Copy and Paste (first characters underlined).
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  14. #14
    VBAX Contributor Daxton A.'s Avatar
    Joined
    Jun 2004
    Location
    Biloxi, Mississippi
    Posts
    143
    Location

    Oh OK

    I gotcha.

  15. #15
    VBAX Newbie
    Joined
    Nov 2007
    Posts
    1
    Location
    Is there any way of doing this on a textbox on a worksheet, rather that on a userform ?

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Start a new thread, this is a tad old and jaded.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  17. #17
    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."
    Last edited by mikepfly2; 12-17-2019 at 11:56 AM. Reason: more detail

  18. #18
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Attachments fall into the abyss of time after a bit.. Can't allow our severs to become full.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •