Search code examples
c#winformscontextmenustriptoolstripitem

Deselect ToolStripItem on ContextMenuStrip


Simple question:

I can .Select a ToolStripItem (like, if i want a preselected option when opening a context menu strip) but i cannot seem to find a way to set .Selected to false or somehow deselect it!

Is it possible?


Solution

  • There is private method ClearAllSelections in ToolStrip class, which removes selections from items. You can invoke it via reflection:

    MethodInfo method = typeof(ToolStrip).GetMethod("ClearAllSelections", BindingFlags.NonPublic | BindingFlags.Instance);
    method.Invoke(yourContextMenuStrip, null);
    

    All selections will be removed.