Erratic behavior from ContextMenuStrip

回眸只為那壹抹淺笑 提交于 2019-12-24 02:09:56

问题


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;
 }
  1. 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.
  2. If a context menu is visible, and an item is selected, the item details do not change.
  3. 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?


回答1:


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.

1. The item in the list view is not always properly selected. In other words, when the item is selected, a detail string value is displayed in a label control.

You can be notified when an item has been selected via the ListView.ItemSelectionChanged event:

//
// this handler's only responsibility is updating the item info label:
//
void lstModules_ItemSelectionChanged(object sender,
                                     ListViewItemSelectionChangedEventArgs e)
{
    if (e.IsSelected)
    {
        // an item has been selected; update the label, e.g.:
        lblModuleDetails.Text = e.Item.Text;
    }
    else
    {
        // some item has been de-selected; clear the label:
        lblModuleDetails.Text = string.Empty;
    }
}

3. Context menu location briefly appears at the old mouse location then moves to the new mouse location.

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 calling ContextMenuStrip.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.

//
// this handler's only responsibility is setting the correct context menu:
//
void lstModules_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        var hitTest = lstModules.HitTest(e.Location);
        if (hitTest != null && hitTest.Item != null)
        {
            lstModules.ContextMenuStrip = mnuContext_Module;
        }
        else
        {
            lstModules.ContextMenuStrip = mnuContext_Desktop;
        }
    }
}

Btw., if that works, you can also get rid of your lstModules_MouseMove event handler and the mouse location object.



来源:https://stackoverflow.com/questions/2212705/erratic-behavior-from-contextmenustrip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!