Solved

rightclicking in a list box

Posted on 2006-11-13
10
277 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
changePi Challenge 15 104
array6 challenfge 6 81
Program to display an alert on Windows Toolbar 2 71
simplest php form 3 61
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now