views:

170

answers:

1

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  A: 

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.

stakx
Clearly, you are a genius. I guess I couldn't see the forest for the trees in this case...thx!!!
dboarman
You're welcome, dboarman!
stakx