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
itself boils down to being a specialized collection of
The nature of a
is such that it can represent a toolbar button, or a menu item. A menu item can optionally have subitems. This makes it a
. 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
event handler for the form, and inside that handler, I added a handler for the first menu item that handled that item's
3) I then added a line in the MouseHover handler that cast the
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
, which has a
() method. So I stopped the debug session, and tried to make the sub menu drop down by calling
. 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
, 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
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.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)
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.