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.
Printable View
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.
Yeah create a ContextMenuStrip and show it at the position of the mouse when the user right clicks the 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
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
vb Code:
If ListBox1.SelectedIndex = -1 Then Me.ListBox1.ContextMenuStrip = Nothing Else Me.ListBox1.ContextMenuStrip = Me.ContextMenuStrip1 End If
That will probably do it.
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.
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
excellent, that works perfectly. Thank you.
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