winforms
winforms copied to clipboard
Hiding a ToolStripMenuItem on DropDownClosed throws exception if closed by hovering over different menu item
.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
- Have a
MenuStripwith at least one visible and one hiddenToolStripMenuItem. Have an event listener forDropDownClosedon the hidden item which sets that item'sVisibleproperty tofalse. - Programatically trigger the hidden menu item by first setting
menuItem.Visible = trueand then callingmenuItem.ShowDropDown(). - Hover over another menu item, thus closing the (intially) hidden item's dropdown.
- A
System.ArgumentOutOfRangeExceptionwill be thrown.
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!
Opened #7957
Verified with .NET SDK 8.0.100-alpha.1.23061.8 build from main branch, this issue is fixed that no exception be thrown.
