Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2004

Tip: Looking for answers? Try searching our database.

Hiding a ContextMenu (or preventing it from showing)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
- 15 Dec 2004 13:53 GMT
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
- 16 Dec 2004 18:53 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.