Consulting

Results 1 to 19 of 19

Thread: Context Menu on UserForm

  1. #1

    Question Context Menu on UserForm

    I apologize if this is a rehash, but the only code I found was from 2007.

    I have the following code running on the right-click event of a user form. The context menu appears, and the buttons can be clicked, but they produce no message box nor any other code I have attempted on the OnAction.

    Private Sub Call_Txt_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
         If Button = 2 Then
              ThisWorkbook.MakePopUp
              Application.CommandBars("MyPopUp").ShowPopup
         End If
    End Sub
     
    Sub MakePopUp(obj As Object)
          'Remove any old instance of MyPopUp
          Excel.Application.CommandBars("MyPopUp").Delete
          With Excel.Application.CommandBars.Add(Name:="MyPopUp", Position:=msoBarPopup)
                With .Controls.Add(Type:=msoControlButton)
                     .OnAction = "'" & ThisWorkbook.Name & "'!" & "Coyp"
                     .FaceId = 19
                     .Caption = "Copy"
                End With
                With .Controls.Add(Type:=msoControlButton)
                     .OnAction = "Paste"
                      .FaceId = 21
                      .Caption = "Paste"
                 End With
          End With
     End Sub
     
     Sub Coyp(obj As Object)
           MsgBox "DataForm " & obj.Name
     End Sub


    I have included these together for ease of reading.

    Call_Txt_MouseUp is called within the UserForm, the rest is in ThisWorkbook.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    in the userform code-module:
    Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
         If Button = 2 Then
              MakePopUp
              Application.CommandBars("MyPopUp").ShowPopup
         End If
    End Sub
    In a Standard code-module:

    Sub Coyp()  '(obj As Object)
         MsgBox "DataForm " ' & obj.Name
    End Sub
    
    Sub MakePopUp()  '(obj As Object)
         'Remove any old instance of MyPopUp
         Excel.Application.CommandBars("MyPopUp").Delete
         With Excel.Application.CommandBars.Add(Name:="MyPopUp", Position:=msoBarPopup)
              With .Controls.Add(Type:=msoControlButton)
                  .OnAction = "'" & ThisWorkbook.Name & "'!" & "Coyp" '"Coyp"  '
                   .FaceId = 19
                   .Caption = "Copy"
              End With
              With .Controls.Add(Type:=msoControlButton)
                   .OnAction = "Paste"
                   .FaceId = 21
                   .Caption = "Paste"
              End With
         End With
    End Sub
    For the moment, I've commented out the obj you seem to be trying to pass around, because I'm not sure what the intention is (are you trying to get this working with multiple textboxes on the userform by using a class module?). As it stands it works on a right-click of the text box called TextBox1
    As you can see my textbox's name was TextBox1
    Private Sub TextBox1_MouseUp(ByVal B…
    You should make sure the event handler is properly matched to your textbox too.
    I'm guessing you got this code from the internet? Can you provide a link?
    Oh, and what version of Excel are you using?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    For others, cross-posted a couple of weeks ago at
    http://www.ozgrid.com/forum/showthread.php?t=174651
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    Yeah the obj was because it didn't seem to be passing the ref... :S

    I am using Excel 2010. I did get it from the internet...
    Ozgrid/forum/showthread.php?t=22696

    sorry about the obfuscation... I cannot post links.

    It does not seem to be crawling over to the .OnAction Event when I click.

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Are you wanting this to work on just a given textbox or several textboxes on the userform? If neither of the above - where?
    What are you wanting to Copy?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    I have 3 textboxes on the user form. It needs to detect on which it was activated and execute the command on that textbox. for example,it knows if I right-clicked on Call_Txt and can copy or cut the value residing there, or paste into it

  7. #7
    Are there any suggestions? I am at my wit's end and about to tell the client the dreaded statement that I cannot fulfill this ask.

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    .OnAction calls a macro, therefore you need macros named like CopyMe and PasteMe, which C&P ThisWorkbook.

    .OnAction = CopyMe

    Sub CopyME()
         ThisWorkbook.Copy
    End Sub
    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

  9. #9
    Thanks for replying, however as I said earlier, it would need to detect the text box that was clicked and run the copy and paste operations on those objects. Correct me if I am wrong but it does not seem like that code would execute a copy person on the userform, much less a child object on that userform.

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    It seems rather hard to return the calling object if you do not pass it to the method:

    ThisWorkbook.MakePopUp Call_text
    Sub MakePopUp(obj As Object)

  11. #11
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    I have this 90% done with a class module (adapted this); but what is it that you want to copy? Is it something different depending on which textbox you right-click on?
    And when you choose Paste from the pop-up, what do you want to paste, and where?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  12. #12
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by p45cal
    I have this 90% done with a class module (adapted this); but what is it that you want to copy? Is it something different depending on which textbox you right-click on?
    And when you choose Paste from the pop-up, what do you want to paste, and where?
    Ignore the above, I've just re-read a previous response from you..
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  13. #13
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    In UserForm Code
     'Declare Form Property
        Dim f_TextBoxClicked As String
        Public Prpoety Get_TextBoxClicked() AS String
        TextBoxClicked = f_textBoxClicked
    End Property
    For each clickable TextBox Code

    'Set Form Property Value
    Sub 'On click event
         f_TextBoxClicked = 'Value to pass to Coyp
    In Copy Code to retrieve value

     Value = UserForm(by Name).TextBoxClicked
    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

  14. #14
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Take a look at the code in the attached, in Module8, UserForm3's code-module and Class1 code.
    Attached Files Attached Files
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  15. #15
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    I wonder if the time I spent on this was wasted?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  16. #16
    Sorry, I hadn't gotten notified that there was a response...I will look a this stuff when I get a chance. Again, I apologize.

  17. #17
    This project was scrapped without my input so... all this work was for naught... damn politics

  18. #18
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    p45cal, no your time was not wasted. The code is handy.

  19. #19
    I agree... the code is handy Thank you p45cal. I apologize for the time taken to respond I am a no good slacker

Posting Permissions

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