Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Automatically Display Menu on Hover

4.89/5 (10 votes)
14 Sep 2011CPOL3 min read 59K  
How to automatically display a Winforms menu when the mouse is hovered over an item

A question was posed today in Q/A and was subsequently (and errantly) closed because one user didn't understand what the question was about.

The question was - How do you cause a MenuStrip item to display without having to click an item?

Granted, the question didn't specify what platform, so I assumed it was WinForms. By the time I was done playing around with some code (10 minutes after reading the question), the question had been deleted, along with my answer. So, I'm posting it as a tip/trick, hoping the person who asked the question to begin with sees it. The following is my answer along with some embellishment to actually make it a tip rather than an answer.

---------------------------------------------------------------------------------

To determine how do to do something in .NET, all you have to know is that all top-level objects inherit their functionality from lower-level objects, and that most controls have a series of events that they can emit/handle. This holds true for the MenuStrip control. The MenuStrip itself boils down to being a specialized collection of ToolStripItem objects.

The nature of a ToolStripItem is such that it can represent a toolbar button, or a menu item. A menu item can optionally have subitems. This makes it a ToolStripDropDownItem. Since .NET knows how to magically drop one of these menu items down, the logical assumption is that the drop down can be caused programmatically as well. To discover how to do this is part of programming.

To answer this question, I performed the following steps:

  1. I loaded up a WinForms test app in Visual Studio, and added a MenuStrip control to the form. I then added a couple of standard menu items. I wasn't really interested in hooking any code to the menu, so I merely added them without any embellishments.
  2. I added a Load event handler for the form, and inside that handler, I added a handler for the first menu item that handled that item's MouseHover event.
  3. I then added a line in the MouseHover handler that cast the sender into a ToolStripItem object, and put a breakpoint on that line.
  4. I started the app under the debugger, and when the breakpoint was hit, I observed that the sender was a ToolStripItem, which has a PerformClick() method. So I stopped the debug session, and tried to make the sub menu drop down by calling PerformClick(). No dice, and so it was back to the debugger.
  5. On the 2nd debugger run, I went into the inheritance chain for the sender, and struck gold. It turns out that the underlying type is actually a ToolStripDropDownItem, and I also discovered it had the expected properties that indicated whether or not it had sub items, and other useful info. This is the object I needed to cast the sender parameter to. After that, the rest was easy.

Here's the code I came up with:

C#
//---------------------------------------------------------------------------------
private void Form1_Load(object sender, EventArgs e)
{
    this.menuStrip1.Items[0].MouseHover += new EventHandler(Form1_MouseHover);
}
 
//---------------------------------------------------------------------------------
void Form1_MouseHover(object sender, EventArgs e)
{
    if (sender is ToolStripDropDownItem)
    {
        ToolStripDropDownItem item = sender as ToolStripDropDownItem;
        if (item.HasDropDownItems && !item.DropDown.Visible)
        {
            item.ShowDropDown();
        }
    }
}

The tip here is that you shouldn't be afraid to do some exploring on your own, let Intellisense guide you, and don't be afraid to make a few mistakes along the way.

Mistakes are what you get when you lack experience. Experience is what you get after you've made mistakes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)