Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Right Click Options on the Selected List View Record Only - Winforms C#.NET

alt text http://img413.imageshack.us/img413/9417/snapshotapp.jpg

The ContextMenuStrip tied to the ListView control. However, the right click option (edit) appear where ever i click on the ListView area, this gives me exceptional error because the implementation of edit can only cope with a selected row. I only want it to appear when on a selected row (blue highlighted row). How can i do it?

like image 583
peace Avatar asked Dec 13 '25 07:12

peace


1 Answers

Reset the ContextMenuStrip property back to (none). Implement the MouseUp event handler and use ListView.HitTest() to find out where it was clicked. For example:

    private void listView1_MouseUp(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Right) {
            var loc = listView1.HitTest(e.Location);
            if (loc.Item != null) contextMenuStrip1.Show(listView1, e.Location);
        }
    }
like image 144
Hans Passant Avatar answered Dec 14 '25 19:12

Hans Passant