Link to home
Start Free TrialLog in
Avatar of MorDeRor
MorDeRor

asked on

rightclicking in a list box

I have a MS Word 2003 form, and in it a list box. I would like to be able to rightclick in this list box to paste a value, but as it is right now, rightclicking does not produce a usual menu, so I have to use "ctrl-v" to paste. Is there way to get the functionality I am looking for? Thanks.
Avatar of jay_s5
jay_s5
Flag of United States of America image

You can probably use Visual Basic for Applications (VBA) to capture the MouseUp event (fires when the mouse button is released) for the control you want to change.

I don't have Word 2003. My version of Word doesn't allow the Paste method to a variable or to a list box, but it does allow it with a text box and other controls. So, to use this solution you just add a text box to your user form and set it's visible property to 'false'. For the sake of the example below, call it TextBox1.

In the code for the control's MouseUp event, you would add the code to set the text value of TextBox1 to the contents of the clipboard, and then add the text value of TextBox1 as an item in your ListBox (ListBox1 in the example) using the AddItem method of the ListBox control.

An example follows:

Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
   
    'Set the value of TextBox1 to the contents of the Clipboard
    TextBox1.Paste
   
    If Button = 2 Then
        ListBox1.AddItem TextBox1.Text
    End If

End Sub

Of course, you can add error-capturing code and get fancy with sorting the contents of your ListBox in the same event if you want. You can also use the variables that the MouseUp event includes (the scary-looking stuff in the parentheses following the name of the event) to fine-tune the behavior you want.
Avatar of MorDeRor
MorDeRor

ASKER

Are you saying that there is no simple way to just get the right click menu that is available just about everywhere else in windows?
Hi,

try with this sample code

Clipboard.GetText will contain the data you have copied and when you right click on List box it will copy the data to list box.

Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then List1.AddItem Clipboard.GetText

End Sub
Hi,

  did you tried with the given code? Is it ok? Do let me know.
No,

It wasn't that I just wanted to paste into a list box with a right click, rather I wanted to know if there was a way to get the right click menu in the list box. does that make sense?
It seems like it should be easier to get the right-click menu, but as far as I can tell, your only option if you want a right-click pop-up menu within the list box is to create one yourself. It doesn't seem like my version of Word offers the right-click menu in a list box because none of the typical commands would apply to it.

You could create a form with the options you want on it and have VBA display it when you right-click the list box. That's about the only way I can see to do it.
To popup a menu when you right click on the list box,
1. Create a menu using menu editor at design time with the main menu having visible property as False and the sub menu you want as visible property True.

2. Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
      If Button = vbRightButton Then
          PopupMenu (Name of the menu)
      End If
End Sub

3. Private Sub mnuPaste_Click()
      List1.AddItem Clipboard.GetText
   End Sub


I hope this is what you want.
Hi,

   Is the given sample code useful? Please let me know if its working.
Hi, RupaliV,

sorry I've been away from this problem. Thanks for checking back. I am not sure that I understood your solution. how do I accomplish the first step? I don't think I know of a menu editor in VBA
ASKER CERTIFIED SOLUTION
Avatar of jay_s5
jay_s5
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial