Erroneous behavior from ContextMenuStrip

I get some erratic behavior from 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 selected correctly when the context menu is displayed. In other words, when an item is selected, the string value of the part is displayed in the label control.
  • If the context menu is visible and the item is selected, the item data is not changed.
  • The context menu location is briefly displayed in the old mouse folder, and then moves to the new mouse location.

Is there something I'm doing wrong with context menus?

+2
source share
1 answer

I tried to reproduce your problem as much as I could. I think I can help you with at least two of the three problems you listed.

1. The list item is not always correctly selected. In other words, when an item is selected, the string value of the part is displayed in the label control.

You can receive notifications when an item has been selected using the ListView.ItemSelectionChanged event:

 // // this handler 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, eg: lblModuleDetails.Text = e.Item.Text; } else { // some item has been de-selected; clear the label: lblModuleDetails.Text = string.Empty; } } 

3. The location of the context menu is briefly displayed in the old mouse folder, and then moves to the new location of the mouse.

I believe you are trying to do too much. Let the structure handle the context menu display that you specified using the ListView.ContextMenuStrip property. The effect that you experience is caused by your manual call to ContextMenuStrip.Show(...) , which causes the context menu to be displayed using the framework, and then you do the same a second time in another place.

Therefore, try not to call this function; a context menu should appear.

 // // this handler 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 this works, you can also get rid of the lstModules_MouseMove event lstModules_MouseMove and the mouse location object.

+2
source

Source: https://habr.com/ru/post/1301983/


All Articles