Skip to content

Commit

Permalink
fix ToolStripItem SelectedChanged event is invoked twice (dotnet#10514)
Browse files Browse the repository at this point in the history
* fix ToolStripItem SelectdChanged event is  invoked twice

* modify unit test
  • Loading branch information
Epica3055 authored Dec 22, 2023
1 parent f05e8f3 commit d44bc19
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -3651,8 +3645,6 @@ internal void Unselect()

KeyboardToolTipStateMachine.Instance.NotifyAboutLostFocus(this);
}

OnSelectedChanged(EventArgs.Empty);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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
Expand Down

0 comments on commit d44bc19

Please sign in to comment.