Click here to Skip to main content
15,884,237 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

Automatically Display Menu on Hover

Rate me:
Please Sign up or sign in to vote.
4.89/5 (10 votes)
14 Sep 2011CPOL3 min read 57.9K   4   4
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)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
SuggestionNice! Pin
blodgyblodgy18-May-17 4:01
blodgyblodgy18-May-17 4:01 
GeneralWorks for me Pin
Paul_Westerman11-Mar-14 23:42
Paul_Westerman11-Mar-14 23:42 
QuestionAutomatically Display Menu on Hove Pin
rimjhim_414-Sep-12 20:13
rimjhim_414-Sep-12 20:13 
AnswerRe: Automatically Display Menu on Hove Pin
#realJSOP24-Mar-16 8:46
mve#realJSOP24-Mar-16 8:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.