|
-
Mar 13th, 2007, 09:10 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Right click menu in a listbox
Hi All,
Is it possible to right click data in a listbox and have a menu display predefined options to do to that selected listbox text?
For example,
I click on data in a list box, and I'll have an option to copy the text to my clipboard.
-
Mar 13th, 2007, 09:34 AM
#2
Re: Right click menu in a listbox
Yeah create a ContextMenuStrip and show it at the position of the mouse when the user right clicks the listbox.
-
Mar 13th, 2007, 10:01 AM
#3
Thread Starter
Addicted Member
Re: Right click menu in a listbox
Thanks Atheist,
I got it to work by adding the ContextMenuStrip as you said. Then I did the below:
Assuming you have a Form1 with a ListBox1 and a ContextMenuStrip1, you can use Properties window to set ListBox1's ContextMenuStrip property to ContextMenuStrip1. The code for this is
Me.ListBox1.ContextMenuStrip = Me.ContextMenuStrip1
I made the below code to work so that you have to left click the item, the right click to load the ContectMenuStrip:
Code:
Private Sub ListBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick
Me.ListBox1.ContextMenuStrip = Me.ContextMenuStrip1
End Sub
Last edited by Giraffe Frenzy; Mar 13th, 2007 at 10:06 AM.
-
Mar 13th, 2007, 10:57 AM
#4
Thread Starter
Addicted Member
Re: [RESOLVED] Right click menu in a listbox
Question,
The below code works great, i left click to get the SelectedItem, then I right click to show the ContextMenuStrip.
Code:
Private Sub ListBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick
Me.ListBox1.ContextMenuStrip = Me.ContextMenuStrip1
End Sub
My problem is that after the Me.ContextMenuStrip1 is set, the right click shows the ContextMenuStrip shows each time I right click even though I do not have a SelectedItem. How do I stop the ContextMenuStrip from showing when I right click except when I have a SelectedItem in the listbox? I am assuming i need to set the ContextMenuStrip for the listbox back to blank, but I do not know how. I tried the below code, but it does not work:
Code:
If ListBox1.SelectedItem = Nothing Then
Me.ListBox1.ContextMenuStrip = Nothing
Else
Me.ListBox1.ContextMenuStrip = Me.ContextMenuStrip1
End If
-
Mar 13th, 2007, 11:38 AM
#5
Fanatic Member
Re: [RESOLVED] Right click menu in a listbox
vb Code:
If ListBox1.SelectedIndex = -1 Then
Me.ListBox1.ContextMenuStrip = Nothing
Else
Me.ListBox1.ContextMenuStrip = Me.ContextMenuStrip1
End If
That will probably do it.
VB.Net 2008
.Net Framework 2.0
"Must you breathe? 'Cause I need heaven..."
-
Mar 13th, 2007, 12:22 PM
#6
Thread Starter
Addicted Member
Re: [RESOLVED] Right click menu in a listbox
This sort of works, I have to click inside the listbox (not selecting anything) for it to not show the ContextMenuStrip. If there is nothing at all selected in the listbox and i right cick, the ContextMenuStrip still shows.
-
Mar 13th, 2007, 01:15 PM
#7
Re: [RESOLVED] Right click menu in a listbox
Use the mousedown event of the listbox, and check for the mouse button. If it's the right button then count the selected items and set the contextmenustrip arcordingly
vb Code:
Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
If ListBox1.SelectedIndices.Count > 0 Then
ListBox1.ContextMenuStrip = Me.ContextMenuStrip1
Else
ListBox1.ContextMenuStrip = Nothing
End If
End If
End Sub
-
Mar 13th, 2007, 01:26 PM
#8
Thread Starter
Addicted Member
Re: [RESOLVED] Right click menu in a listbox
excellent, that works perfectly. Thank you.
-
May 2nd, 2024, 11:43 PM
#9
New Member
Re: [RESOLVED] Right click menu in a listbox
Actually, it's easiest to include....
Private Sub List1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List1.SelectedIndexChanged
If List1.SelectedIndex = -1 Then List1.ContextMenuStrip = Nothing Else List1.ContextMenuStrip = ContextMenuStrip1
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|