Click here to Skip to main content
Click here to Skip to main content

Automatically Display Menu on Hover

, 14 Sep 2011
Rate this:
Please Sign up or sign in to vote.
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 spoecify 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 erather 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 programatically as well. To discover how to do this is part of programming.
 
To answer this question, I performed the following steps:
 
0) 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:
 
//---------------------------------------------------------------------------------
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)

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
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

 
GeneralWorks for me PinmemberPaul_Westerman11-Mar-14 23:42 
QuestionAutomatically Display Menu on Hove Pinmemberrimjhim_414-Sep-12 20:13 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 14 Sep 2011
Article Copyright 2011 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid