Search code examples
c#winformsdrop-down-menutooltip

Menu item's tooltip hidden by submenu dropdownlist


I have a winforms ContextMenuStrip that has ToolTipText set for all of it's ToolStripMenuItem's.

One of these menu items, "Insert Action", has an associated ToolStripDropDown sub-menu. When the mouse is moved to Insert Action, the drop down menu is automatically displayed and the tooltip associated with Insert Action is displayed but pushed to the background behind both the ContextMenuStrip and the dropdown.

See here for a picture of the problem: http://www.screencast.com/t/GZkeBNcU

I have tried programatically re-selecting Insert Action after the sub-menu is opened, but the tooltip will not redisplay on top.

Any ideas?

Alternatively is there a way to only display Insert Action's sub-menu when it is clicked (as opposed to automatically when the mouse is moved over it)? I would think there should be a setting for this, but haven't been able to find it.

Thanks for your help.


Solution

  • Ok, this isn't terribly elegant so bear with me.

    It seems as though the ToolTipText is being drawn on top initially, but once the next ToolStripMenuItems load(The ones brought about by "Insert Action" in your case), both sets of MenuStrips find their way above the ToolTipText. So, my motivation was to force the ToolTipText to appear AFTER the new MenuStrip loads.

    So, first I added a DropDownOpened handler to myToolStripMenuItem which would be whatever you named your "Insert Action..." MenuItem.

    Then, I added the following code

    private void myToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
    {
        myToolStripMenuItem.Visible = false;
        myToolStripMenuItem.Visible = true;
        myToolStripMenuItem.ToolTipText = "Tooltip info that you want to see!";
    }
    

    What this does is waits for the sub-menu to load, toggles the visibility of myToolStripMenuItem (which is the ugly part in all this), and resets the ToolTipText, forcing it to display after everything is loaded and also on top of everything.

    However, leaving it like this would result in some flashing of text if you were to attempt to hover and leave twice or more. Give it a shot with just this code and you will see what I mean. So, you have to reset the ToolTipText to a blank string. I did this when the same MenuItem's DropDownClosed event fires.

    private void myToolStripMenuItem_DropDownClosed(object sender, EventArgs e)
    {
        myToolStripMenuItem.ToolTipText = "";
    }
    

    Now, the ToolTipText appears exactly when and where we want it to appear without ever flickering.

    NOTE: This method will result in the ToolTipText taking a little bit longer to show up than normal. This is, of course, because the code waits for the dropdown menu to load. Also, toggling myToolStripMenuItem.Visible will occasionally cause a flicker in the menu. However, I feel that is significantly better than showing a ToolTipText that you can't quite see.