I am getting some erratic behavior from a ContextMenuStip
:
private void lstModules_MouseMove(object sender , MouseEventArgs e)
{ mouse = e.Location; }
private void lstModules_MouseDown(object sender , MouseEventArgs e)
{
ListViewItem item = null;
if((hitTest = lstModules.HitTest(mouse)) != null)
item = hitTest.Item;
switch (e.Button)
{
case MouseButtons.Right:
if (item != null)
{
// valid item selection
ShowModuleDetails(item.Name);
lstModules.ContextMenuStrip = mnuContext_Module;
}
else
{
// right-click - no item selection
lblModuleDetails.Text = string.Empty;
lstModules.ContextMenuStrip = mnuContext_Desktop;
}
lstModules.ContextMenuStrip.Show(lstModules , mouse);
break;
case MouseButtons.Left:
if (item != null)
{ ShowModuleDetails(item.Name); }
break;
}
}
private void ShowModuleDetails(string modName)
{
// get module details from dictionary
lblModuleDetails.Text = Modules[modName].Details;
}
- The item in the list view is not properly selected when the context menu is showing. In other words, when the item is selected, a detail string value is displayed in a label control.
- If a context menu is visible, and an item is selected, the item details do not change.
- Context menu location briefly appears at the old mouse location then moves to the new mouse location.
Is there something I'm doing wrong with the context menus?
I tried to reproduce your problem as far as I could. I think I can help you out with at least two of the three issues you've listed.
You can be notified when an item has been selected via the
ListView.ItemSelectionChanged
event:I believe you try to do too much. Let the framework handle the displaying of the context menu which you have specified via the
ListView.ContextMenuStrip
property. The effect you experience is caused by your manually callingContextMenuStrip.Show(...)
, which results in the displaying of the context menu by the framework, and then you doing the same thing a second time, at another location.Therefore, try not to call this function; the context menu should still appear.
Btw., if that works, you can also get rid of your
lstModules_MouseMove
event handler and themouse
location object.