winforms icon indicating copy to clipboard operation
winforms copied to clipboard

Hiding a ToolStripMenuItem on DropDownClosed throws exception if closed by hovering over different menu item

Open jonaskohl opened this issue 3 years ago • 1 comments

.NET version

Version: 6.0.300 Commit: 8473146e7d

Also did not work in .NET Framework 4.8

Did it work in .NET Framework?

No

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No, it did not work in any of the earlier releases of .NET Core or .NET 5+.

Issue description

Exception stack trace
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   at System.Collections.ArrayList.get_Item(Int32 index)
   at System.Windows.Forms.ToolStripItemCollection.get_Item(Int32 index)
   at System.Windows.Forms.ToolStrip.ClearAllSelectionsExcept(ToolStripItem item)
   at System.Windows.Forms.ToolStrip.NotifySelectionChange(ToolStripItem item)
   at System.Windows.Forms.ToolStripItem.Select()
   at System.Windows.Forms.ToolStripItem.HandleMouseEnter(EventArgs e)
   at System.Windows.Forms.ToolStrip.OnMouseMove(MouseEventArgs mea)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)
Screen recording of the issue

https://user-images.githubusercontent.com/23175424/193817496-069210c5-edd2-4014-903f-e68a3e9e67d1.mp4

Test VS solution to reproduce (includes .NET 6 and .NET Framework 4.8 projects)


Problem is probably caused by a race condition in ToolStrip.ClearAllSelectionsExcept(ToolStripItem).

Steps to reproduce

  1. Have a MenuStrip with at least one visible and one hidden ToolStripMenuItem. Have an event listener for DropDownClosed on the hidden item which sets that item's Visible property to false.
  2. Programatically trigger the hidden menu item by first setting menuItem.Visible = true and then calling menuItem.ShowDropDown().
  3. Hover over another menu item, thus closing the (intially) hidden item's dropdown.
  4. A System.ArgumentOutOfRangeException will be thrown.

jonaskohl avatar Oct 04 '22 12:10 jonaskohl

This issue is now marked as "help wanted", and we’re looking for a community volunteer to work on this issue. If we receive no interest in 180 days, we will close the issue. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

ghost avatar Oct 11 '22 23:10 ghost

Opened #7957

jonaskohl avatar Oct 17 '22 20:10 jonaskohl

Verified with .NET SDK 8.0.100-alpha.1.23061.8 build from main branch, this issue is fixed that no exception be thrown. 7884-testresult

John-Qiao avatar Jan 12 '23 03:01 John-Qiao