Hiding/Closing ContextMenu

E

[EVA-02]Tseng

Hi,
got a problem with Contextmenu. I got a ListView and want to have the ContextMenu only shown when the user rightclicks an Element
in the Listview. If ther is no element under the mouse cursor no menu shall appear.

The source looks like

private void lstMaterials_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
if(e.Button == MouseButtons.Right){
Point p = new Point(e.X,e.Y);
ListViewItem lv = lstMaterials.GetItemAt(p.X,p.Y);
if(lv!=null){
lv.Selected = true;
conMatMenu.Show(lstMaterials,p);
}
}
}

So far so good, menu only appears when a ListViewItem is below the mouse cursor. But if i right click another Item, the menu just
closes. So i have to click twice on the element (once to close it and once to open the context for the new menu). I failed to find
a way to close an already opened ContextMenu before doing the check if an ListViewItem is below the mousecursor.

Any ideas?
 
A

AlanT

A possibility is to use the mousedown event to set the context menu
property of the listview. The standard context menu mechanism will
then kick in, no need to expliclty show the menu

e.g.

private void lstMaterials_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e) {

ListView lvw = (ListView)sender;

if(e.Button == MouseButtons.Right){

ListViewItem lv = lstMaterials.GetItemAt(e.X,e.Y);
if(lv!=null){
lv.Selected = true;
lvw.ContextMenu = conMatMenu;
else {
lvw.ContextMenu = nothing;
}
}
}


hth,
Alan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top