From d44bc19572078e11155823131c7b697359315b04 Mon Sep 17 00:00:00 2001 From: Epica3055 <135201996+Epica3055@users.noreply.github.com> Date: Sat, 23 Dec 2023 01:45:28 +0800 Subject: [PATCH] fix ToolStripItem SelectedChanged event is invoked twice (#10514) * fix ToolStripItem SelectdChanged event is invoked twice * modify unit test --- .../Forms/Controls/ToolStrips/ToolStripItem.cs | 8 -------- .../System/Windows/Forms/ToolStripItemTests.cs | 18 ++++++++++-------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripItem.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripItem.cs index 0320bddfce1..7055c33d1c9 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripItem.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripItem.cs @@ -2587,16 +2587,10 @@ private void HandleLeave() { if (_state[s_stateMouseDownAndNoDrag] || _state[s_statePressed] || _state[s_stateSelected]) { - bool wasSelected = _state[s_stateSelected]; _state[s_stateMouseDownAndNoDrag | s_statePressed | s_stateSelected] = false; KeyboardToolTipStateMachine.Instance.NotifyAboutLostFocus(this); - if (wasSelected) - { - OnSelectedChanged(EventArgs.Empty); - } - Invalidate(); } } @@ -3651,8 +3645,6 @@ internal void Unselect() KeyboardToolTipStateMachine.Instance.NotifyAboutLostFocus(this); } - - OnSelectedChanged(EventArgs.Empty); } } diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripItemTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripItemTests.cs index b6abd1434b5..719e04bd108 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripItemTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripItemTests.cs @@ -15400,23 +15400,23 @@ public void ToolStripItem_IDropTargetOnDragOver_Invoke_CallsDragOver(DragEventAr // Unit test for https://github.com/dotnet/winforms/issues/8548 [WinFormsFact] - public void ToolStripItem_OnItemSelected() + public void ToolStripItem_OnItemSelectedChanged() { using MyMenuStrip menuStrip1 = new(); using ToolStripMenuItem toolStripMenuItem1 = new(); using ToolStripMenuItem toolStripMenuItem2 = new(); using ToolStripMenuItem toolStripMenuItem3 = new(); - menuStrip1.Size = new Size(50, 100); - toolStripMenuItem1.Size = new Size(15, 30); + menuStrip1.Size = new Size(100, 50); + toolStripMenuItem1.Size = new Size(10, 30); toolStripMenuItem2.Size = new Size(15, 30); toolStripMenuItem3.Size = new Size(15, 30); - bool callBackInvoked = false; + int callBackInvokedCount = 0; toolStripMenuItem2.SelectedChanged += (e, s) => { - callBackInvoked = true; + callBackInvokedCount++; }; menuStrip1.Items.AddRange(new ToolStripMenuItem[] { toolStripMenuItem1, toolStripMenuItem2, toolStripMenuItem3 }); @@ -15429,14 +15429,16 @@ public void ToolStripItem_OnItemSelected() menuStrip1.MoveMouse(new MouseEventArgs(MouseButtons.None, 0, new Point(i, 5))); } - Assert.False(callBackInvoked); + Assert.Equal(0, callBackInvokedCount); - for (int i = 10; i < 50; i++) + for (int i = 10; i < 100; i++) { menuStrip1.MoveMouse(new MouseEventArgs(MouseButtons.None, 0, new Point(i, 5))); } - Assert.True(callBackInvoked); + // SelectedChanged event should be fired once in one round. + + Assert.Equal(1, callBackInvokedCount); } private class MyMenuStrip: MenuStrip