Windows Develop Bookmark and Share   
 index > Windows Forms General > Dropdownitems of a disabled ToolstripMenuItem can still be selected
 

Dropdownitems of a disabled ToolstripMenuItem can still be selected

see https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=235911

also this post is probably related: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=312139&SiteID=1

I currently use the EnabledChanged event to disable all the DropDownItems (recursive), but this doesn't prevent the items from 'popping up'.

Sven De Bont  Wednesday, December 06, 2006 3:00 PM
After digging around with Reflector a while, I think I discovered the bug. There's an internal static timer that delays the downdown list popping up. That timer doesn't check if the menu item is enabled. I figured out a way to cancel that timer through reflection:

private void cancelMenuTimer(ToolStripMenuItem item) {
// Disable the internal static MenuTimer
Type t = typeof(ToolStripMenuItem);
FieldInfo fi = t.GetField("menuTimer", BindingFlags.Static | BindingFlags.NonPublic);
object tmr = fi.GetValue(item);
Type ti = tmr.GetType();
MethodInfo mi = ti.GetMethod("CancelCore", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(tmr, null);
}

That worked great and suppressed the popup. Then I hit the wall. When a menu item is disabled, it doesn't generate any of the events so there's no easy way to call cancelMenuTimer(). Particularly, MouseEnter was the one I was trying to use. I threw in the towel on that one. Sorry for the crummy answer, hopefully somebody else has an idea...
nobugz  Wednesday, December 06, 2006 5:04 PM

Your comment got me thinking & nosing around with Reflector again. I don't think it was intended for the timer to check if the item is disbled. It looks like the class was desinged to only enable the timer in the OnMouseEnter when it is not enabled, and to disable the timer (cancel) in the OnMouseLeave event. For some reason (which I cannot find), the timer does get enabled for disabled items when you move the mouse over it coming from an allready expanded item.

I did notice that the OnMouseEnter does check if this.Selected is true before enabling the timer. The Selected property in turn, will check this.CanSelect which always returns true (and is protected virtual). So by overring the CanSelect property, you can avoid the menu from expanding

class ToolStripItemEx : System.Windows.Forms.ToolStripMenuItem{

public override bool CanSelect {

get {

return this.Enabled;

}

}

}

Note that this however does NOT prevent the user from executing the click action of any of the childitems that have a shortcutkey by pressing that key (unless the childitems is disabled) as described in this porst: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=312139&SiteID=1

It might be possible to work around this issue by overring the click of the item and making sure the parent (and the parent's parent, ect) is not disabled.

Regards

Sven De Bont  Thursday, December 07, 2006 1:10 PM
Just wanted to mention that the MainMenu component doesn't have this particular problem.

Also, it's a little strange that DropDownOpening event isn't cancellable.  The class library guidelines indicate that the gerund form (-ing) should be used when an event can be cancelled...
arnshea.blogspot.com
Arnshea  Wednesday, October 07, 2009 6:53 PM

You can use google to search for other answers

Custom Search

More Threads

• Linking multiple SQL Data Tables
• Activex Com control properties
• Is there an easy way to sent a DataGridView to the printer?
• TreeView - order of events and lazy loading of data
• Customizing AxWebBrowser content while printing
• Folder Browser Dialog -- Disk Error
• ContextMenuStrip & MenuStrip fading
• MenuItem appears on the top left corner of the screen instead under the menu.
• Tinted transparency in part of a From
• Change icon sizes programatically