Solved

rightclicking in a list box

Posted on 2006-11-13
10
281 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:MorDeRor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 2

Expert Comment

by:jay_s5
ID: 17934825
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.
0
 
LVL 3

Author Comment

by:MorDeRor
ID: 17936013
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?
0
 
LVL 1

Expert Comment

by:RupaliV
ID: 17936592
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
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 1

Expert Comment

by:RupaliV
ID: 17984958
Hi,

  did you tried with the given code? Is it ok? Do let me know.
0
 
LVL 3

Author Comment

by:MorDeRor
ID: 17984997
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?
0
 
LVL 2

Expert Comment

by:jay_s5
ID: 17989859
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.
0
 
LVL 1

Expert Comment

by:RupaliV
ID: 18005886
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.
0
 
LVL 1

Expert Comment

by:RupaliV
ID: 18290148
Hi,

   Is the given sample code useful? Please let me know if its working.
0
 
LVL 3

Author Comment

by:MorDeRor
ID: 18294908
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
0
 
LVL 2

Accepted Solution

by:
jay_s5 earned 500 total points
ID: 18295193
I think RupalIV is talking about VB, where there is a menu editor and a PopupMenu method. I don't think you have that option in Word/VBA, unfortunately.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Introduction to Processes
Starting up a Project

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question