Solved

rightclicking in a list box

Posted on 2006-11-13
10
276 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
seriesUp challenge 7 107
tidtcpserver connection lost handle 2 49
Device same like our heart 12 49
C# code editing and collaboration 3 49
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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

746 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

7 Experts available now in Live!

Get 1:1 Help Now