Skip to content

Commit

Permalink
[API Proposal]: add ToolStripItem.SelectedChanged event dotnet#8548 (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
Epica3055 authored Dec 14, 2023
1 parent 658e6be commit 22ab786
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/System.Windows.Forms/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ System.Windows.Forms.ListViewGroupCollection.AddRange(params System.Windows.Form
System.Windows.Forms.ListViewItem.ListViewSubItemCollection.AddRange(params string![]! items) -> void
System.Windows.Forms.ListViewItem.ListViewSubItemCollection.AddRange(params System.Windows.Forms.ListViewItem.ListViewSubItem![]! items) -> void
System.Windows.Forms.TabControl.TabPageCollection.AddRange(params System.Windows.Forms.TabPage![]! pages) -> void
System.Windows.Forms.ToolStripItem.SelectedChanged -> System.EventHandler?
System.Windows.Forms.ToolStripItemCollection.AddRange(params System.Windows.Forms.ToolStripItem![]! toolStripItems) -> void
System.Windows.Forms.ToolStripPanel.ToolStripPanelRowCollection.AddRange(params System.Windows.Forms.ToolStripPanelRow![]! value) -> void
virtual System.Windows.Forms.Control.ControlCollection.AddRange(params System.Windows.Forms.Control![]! controls) -> void
virtual System.Windows.Forms.ListView.ColumnHeaderCollection.AddRange(params System.Windows.Forms.ColumnHeader![]! values) -> void
virtual System.Windows.Forms.ToolStripItem.OnSelectedChanged(System.EventArgs! e) -> void
virtual System.Windows.Forms.TreeNodeCollection.AddRange(params System.Windows.Forms.TreeNode![]! nodes) -> void
3 changes: 3 additions & 0 deletions src/System.Windows.Forms/src/Resources/SR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5889,6 +5889,9 @@ Stack trace where the illegal operation occurred was:
<data name="ToolStripItemsDescr" xml:space="preserve">
<value>Collection of items to display on the ToolStrip.</value>
</data>
<data name="ToolStripItemSelectedChangedDescr" xml:space="preserve">
<value>Occurs when the Selected property value changes.</value>
</data>
<data name="ToolStripItemSize" xml:space="preserve">
<value>The size of the item.</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public abstract partial class ToolStripItem :
internal static readonly object s_ownerChangedEvent = new();
internal static readonly object s_paintEvent = new();
internal static readonly object s_textChangedEvent = new();
internal static readonly object s_selectedChangedEvent = new();

internal static readonly object s_commandChangedEvent = new();
internal static readonly object s_commandParameterChangedEvent = new();
Expand Down Expand Up @@ -314,7 +315,7 @@ public ToolStripItemAlignment Alignment
/// <summary>
/// Determines if this item can be dragged.
/// This is EXACTLY like Control.AllowDrop - setting this to true WILL call
/// the droptarget handlers. The ToolStripDropTargetManager is the one that
/// the dropTarget handlers. The ToolStripDropTargetManager is the one that
/// handles the routing of DropTarget events to the ToolStripItem's IDropTarget
/// methods.
/// </summary>
Expand Down Expand Up @@ -845,6 +846,7 @@ public virtual bool Enabled
if (wasSelected)
{
KeyboardToolTipStateMachine.Instance.NotifyAboutLostFocus(this);
OnSelectedChanged(EventArgs.Empty);
}
}

Expand Down Expand Up @@ -1429,7 +1431,7 @@ public event MouseEventHandler? MouseUp

/// <summary>
/// Name of this control. The designer will set this to the same
/// as the programatic Id "(name)" of the control. The name can be
/// as the programmatic Id "(name)" of the control. The name can be
/// used as a key into the ControlCollection.
/// </summary>
[Browsable(false)]
Expand All @@ -1449,7 +1451,7 @@ public string? Name
}

/// <summary>
/// The owner of this ToolStripItem. The owner is essentially a backpointer to
/// The owner of this ToolStripItem. The owner is essentially a backPointer to
/// the ToolStrip who contains this item in it's item collection. Handy for getting
/// to things such as the ImageList, which would be defined on the ToolStrip.
/// </summary>
Expand All @@ -1470,7 +1472,7 @@ public ToolStrip? Owner
}

/// <summary>
/// Returns the "parent" item on the preceeding menu which has spawned this item.
/// Returns the "parent" item on the preceding menu which has spawned this item.
/// e.g. File->Open the OwnerItem of Open is File.
/// </summary>
[Browsable(false)]
Expand Down Expand Up @@ -1819,6 +1821,18 @@ public virtual bool Selected
(ParentInternal is not null && ParentInternal.IsSelectionSuspended &&
ParentInternal.LastMouseDownedItem == this));

/// <summary>
/// Occurs when selected item changed.
/// </summary>
[SRDescription(nameof(SR.ToolStripItemSelectedChangedDescr))]
public event EventHandler? SelectedChanged
{
add => Events.AddHandler(s_selectedChangedEvent, value);
remove => Events.RemoveHandler(s_selectedChangedEvent, value);
}

protected virtual void OnSelectedChanged(EventArgs e) => RaiseEvent(s_selectedChangedEvent, e);

protected internal virtual bool ShowKeyboardCues
=> DesignMode || ToolStripManager.ShowMenuFocusCues;

Expand Down Expand Up @@ -2222,7 +2236,7 @@ public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects, B
iwdata.SetData(data);
}

dataObject = (IComDataObject)iwdata;
dataObject = iwdata;
}

DROPEFFECT finalEffect;
Expand Down Expand Up @@ -2565,10 +2579,16 @@ 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 @@ -3229,6 +3249,8 @@ internal void Select(bool forceRaiseAccessibilityFocusChanged)

KeyboardToolTipStateMachine.Instance.NotifyAboutGotFocus(this);

OnSelectedChanged(EventArgs.Empty);

forceRaiseAccessibilityFocusChanged = true;
}

Expand Down Expand Up @@ -3621,6 +3643,8 @@ internal void Unselect()

KeyboardToolTipStateMachine.Instance.NotifyAboutLostFocus(this);
}

OnSelectedChanged(EventArgs.Empty);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15398,6 +15398,55 @@ public void ToolStripItem_IDropTargetOnDragOver_Invoke_CallsDragOver(DragEventAr
Assert.Equal(1, callCount);
}

// Unit test for https://github.com/dotnet/winforms/issues/8548
[WinFormsFact]
public void ToolStripItem_OnItemSelected()
{
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);
toolStripMenuItem2.Size = new Size(15, 30);
toolStripMenuItem3.Size = new Size(15, 30);

bool callBackInvoked = false;

toolStripMenuItem2.SelectedChanged += (e, s) =>
{
callBackInvoked = true;
};

menuStrip1.Items.AddRange(new ToolStripMenuItem[] { toolStripMenuItem1, toolStripMenuItem2, toolStripMenuItem3 });

menuStrip1.CreateControl();

// try to emulate mouse move event.
for (int i = 0; i < 10; i++)
{
menuStrip1.MoveMouse(new MouseEventArgs(MouseButtons.None, 0, new Point(i, 5)));
}

Assert.False(callBackInvoked);

for (int i = 10; i < 50; i++)
{
menuStrip1.MoveMouse(new MouseEventArgs(MouseButtons.None, 0, new Point(i, 5)));
}

Assert.True(callBackInvoked);
}

private class MyMenuStrip: MenuStrip
{
public void MoveMouse(MouseEventArgs mea)
{
OnMouseMove(mea);
}
}

private class SubToolStrip : ToolStrip
{
public new void OnBeginDrag(EventArgs e) => base.OnBeginDrag(e);
Expand Down

0 comments on commit 22ab786

Please sign in to comment.