[VBA] Right Click Popup Menu from Userform

A few years ago, at my previous employer, I built a VBA macro that had a Right Click Copy/Paste menu for text boxes. Unfortunately, as the code was developed on the company's dime, it had to remain behind when I was downsized.

So far, my current searches are proving uneventful. Does anyone have any examples they are willing to share?

BTW, I was able to create a doubleclick copy to clipboard, but right clicking is far more commonplace and intuitive.

Here is that code:

Private Sub TxtBxNorthing_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Set odata = New DataObject
    odata.Clear
    odata.SetText Me.TxtBxNorthing.Text
    odata.PutInClipboard
End Sub

TIA

Parents Reply
  • From what I can recall of my earlier effort, The popup menu was pretty crude and did not function 100% like a true right click menu, but resembled one enough that users could follow its intent.

    I'm thinking, it might have even been a hidden, raised listbox that I made visible at the cursor position, if that's possible. This was over 5 years ago so it's s pretty distant memory.


    Charles (Chuck) Rheault
    CADD Manager

    MDOT State Highway Administration

    • MicroStation user since IGDS, InRoads user since TDP.
    • AutoCAD, Land Desktop and Civil 3D, off and on since 1996
Children
  • I cannot be sure this is how I did it, as much of what I just tried is not familiar at all. But it appears to be a possibility.

    Created a List Box on my user form using these properties - Key items: BackColor (menu color), Special Effect - Raised, Visible = False, Column width (smaller than text placed in it - prevents scroll bars).

    With userForm code along these lines:

    Private Sub UserForm_Initialize()
        Me.ListBox1.AddItem ("Copy")
        Me.ListBox1.AddItem ("Paste")
    End Sub

    Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        If Button = 2 Then
            Me.ListBox1.ListIndex = -1
            Me.ListBox1.Top = Y - (Me.ListBox1.Height / 2)
            Me.ListBox1.Left = X - (Me.ListBox1.Width / 2)
            Me.ListBox1.Visible = True
            Me.ListBox1.Font.Size = 10
        ElseIf Button = 1 Then
            Me.ListBox1.Visible = False
        End If
    End Sub

    Private Sub ListBox1_Click()
        If Me.ListBox1.ListIndex = 0 Then Debug.Print "Copy to Clipboard"
        If Me.ListBox1.ListIndex = 1 Then Debug.Print "Paste from Clipboard"
        Me.ListBox1.Visible = False
    End Sub

    As you can see, this version only writes to the immediate window - but if that works, my copy to clibboard code will work.

    Initially, I placed the .Top and .Left of the list box at Y and X. But I decided to center the list box on the mouse to keep mouse clicks too near an edge from looking too strange - as the listbox does not really float over the form like a commandbar.


    Charles (Chuck) Rheault
    CADD Manager

    MDOT State Highway Administration

    • MicroStation user since IGDS, InRoads user since TDP.
    • AutoCAD, Land Desktop and Civil 3D, off and on since 1996
  • That screenshot looks like Excel.  As I wrote already, an Office VBA macro that uses the CommandBars object won't work with MicroStation VBA.  MicroStation VBA does not provide a CommandBars object.

     
    Regards, Jon Summers
    LA Solutions