5

We have a form in our program that allows a user to determine what a user can see when setting privileges, by using a menu bar. When a user clicks on an item, that item then is "selected" (gets a tick next to it). However, this also closes the menu.

enter image description here

Is there a way to stop this menu from closing (without affecting any other menu's in the program) when a user clicks on it? So far I have not found anything in the settings, and any _click methods are not affecting it either.

Ben
  • 2,433
  • 5
  • 39
  • 69

4 Answers4

5

Listen the ToolStripDropDownClosingEventHandler present in ToolStripDropDown. To do this just access the ToolStripMenuItem.DropDown Property, and so, add a listener that will handle the Closing event. We just need to check if the mouse pointer it's inside of toolStrip rectangle and this can be done with this piece of code:

private void ListenToolStripMenuItems(IEnumerable<ToolStripMenuItem> menuItems)
{
    // listen all menuItems
    foreach (ToolStrip menuItem in menuItems)
        menuItem.DropDown.Closing += OnToolStripDropDownClosing;
}

private void OnToolStripDropDownClosing(object sender, ToolStripDropDownClosingEventArgs e)
{
        var tsdd = (ToolStripDropDown)sender;

        // checking if mouse cursor is inside
        Point p = tsdd.PointToClient(Control.MousePosition);  
        if (tsdd.DisplayRectangle.Contains(p))
            e.Cancel = true;  // cancel closing
}

This way the AutoClose still working and the toolStrip will close properly.

Philippe
  • 28,207
  • 6
  • 54
  • 78
Henrique
  • 198
  • 5
  • 11
  • 1
    This is a good solution, because it keeps the AutoClose functionality. Turning off AutoClose brings its own problems, in that you have to manually handle closing the menu again. – AeonOfTime May 03 '20 at 13:35
  • Thank You very much, the key was DropDown.Closing event – mdghost Sep 07 '21 at 11:06
2

I'm a hack, but I would do this for each item you can click:

 sampleNameToolStripMenuItem.ShowDropDown();

That way whenever you click something, it will also drop the menu down again right after.

user3263609
  • 21
  • 1
  • 5
1

I had a similar issue. The Items to be checked was a Level down in my case.

I solved it by adding a MouseEnter and MouseLeave Event to the ToolStripMenuItems that can be checked/unchecked.

In the MouseEnter I set AutoClose of both menuItems to false - in the MouseLeave I set it back to true.

Visualization of the issue I had

 private void AddEventToToolStripMenuItems(IEnumerable<ToolStripMenuItem> menuItems)
        {
            // listen all menuItems
            foreach (ToolStripMenuItem menuItem in menuItems)
            {
                menuItem.MouseEnter += OnToolStripMouseEnter;
                menuItem.MouseLeave += OnToolStripMouseLeave;
            }
        }

       private void OnToolStripMouseEnter(object sender, EventArgs e)
        {
            firstLevelTooStipMenuItem.DropDown.AutoClose = false;
            secondLevelTooStipMenuItem.DropDown.AutoClose = false;
        }

        private void OnToolStripMouseLeave(object sender, EventArgs e)
        {
            firstLevelTooStipMenuItem.DropDown.AutoClose = true;
            secondLevelTooStipMenuItem.DropDown.AutoClose = true;
        }
Pwyll
  • 11
  • 3
-1
  1. Get the ToolStripMenuItem

  2. Get the DropDown from ToolStripMenuItem

  3. Set AutoClose as false

Ref:

https://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripmenuitem(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripdropdown(v=vs.110).aspx