Context Menus With Listview

Posted on 2004-08-18
Last Modified: 2008-03-10

Im just trying to tidyup the user interface for the listview on my form.  The problem that im having is, I would like when the users RightClicks on a listviewItem for it to selcted(highlight) it fist before bringing up the contextmenu.


While the contextmenu is displayed I have another anoying bug where if you click anywhere else on the form the contextmenu disapears and reapears very quickly so to make a contextmenu go away without selecting a option from it you have to efectivly click twice anywhere on the form :(

[Code Im using to display the contextmenu on the listview]

    Private Sub listmenu_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        oCurrentNode = sender.GetItemAt(e.X, e.Y)

        'If Right mouse button is pressed
        If e.Button = MouseButtons.Right Then
            If Not Me.ContextMenu Is Nothing Then
                Me.ContextMenu.Show(Me, New Point(e.X, e.Y))
            End If
        End If
    End Sub


Question by:emub
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2

Author Comment

ID: 11829775
oCurrentNode is just a (global) "Private oCurrentNode As ListViewItem" on the form

Author Comment

ID: 11829811

Sorry I solved the first problem with addeding

Me.Items(oCurrentNode.Index).Selected = True

before displaying the contextmenu.

but the second problem is insits on having to click twice anywhere to get rid of the contextmenu :(
LVL 25

Accepted Solution

RonaldBiemans earned 350 total points
ID: 11829819
Instead of using the mousedown use the mouseup event (then it will select the item first)
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 11829846
Ah DOH!,  thx that solved both problems in 1 go dont need that .Slected line in any more :)
LVL 25

Expert Comment

ID: 11829855
you are using mybase.mouse down which is the mousedown event of the form or are you using a custom listview control ?

   Private Sub listmenu_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

shouldn't it be

   Private Sub listmenu_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles listmenu.MouseDown

Author Comment

ID: 11829861
Im using a custom control that inherits ListView.  :)

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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