Hello,
I have a ListBox with column names to use in a report. When the user
right-clicks the ListBox, a ContextMenu is displayed with two items: Sort
Ascending and Sort Descending.
How can I display the ContextMenu only when the ListBox has a selected item?
(All I do now is just disable the menu items if the ListBox's SelectedIndex
is -1.)
Thank you,
Eric
Thomas Weise - 15 Dec 2004 18:25 GMT
Eric,
You have probably assigned the context menu to the ListBox by setting the
ListBox.Contextmenu property.
You should rather launch the context menu manually in the ListBox' MouseDown
procedure by this code line:
ContextMenu.Show(CType(sender, ListBox), New Point(e.X, e.Y))
In this way, you can control if you show the menu at all
Regards,
> Hello,
>
[quoted text clipped - 11 lines]
>
> Eric
Claes Bergefall - 16 Dec 2004 08:14 GMT
You could handle the SelectedIndexChanged event and
add and remove the context menu as appropriate
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedIndex < 0 Then
ListBox1.ContextMenu = Nothing
Else
ListBox1.ContextMenu = MyContextMenu
End If
End Sub
> Hello,
>
[quoted text clipped - 9 lines]
>
> Eric
Thank you both for your replies. They both work great.
Take care,
Eric
"Thomas Weise" <wommel@onlinehome.de> wrote in message
news:OdY52Nt4EHA.1292@TK2MSFTNGP10.phx.gbl...
> Eric,
>
> You have probably assigned the context menu to the ListBox by setting the
> ListBox.Contextmenu property.
> You should rather launch the context menu manually in the ListBox'
MouseDown
> procedure by this code line:
> ContextMenu.Show(CType(sender, ListBox), New Point(e.X, e.Y))
> In this way, you can control if you show the menu at all
>
> Regards,
> Thomas
"Claes Bergefall" <claes.bergefall@online.nospam> wrote in message
news:%237XIHd04EHA.3472@TK2MSFTNGP09.phx.gbl...
> You could handle the SelectedIndexChanged event and
> add and remove the context menu as appropriate
[quoted text clipped - 23 lines]
>
> Eric