Welcome to MSDN Blogs Sign in | Join | Help

jfo's coding

adventures in windows forms and wpf
Workaround for disabled flyouts

Frans ran into some trouble disabling parent menu items.  Turns out they dont disable the evaluation of child shortcuts.  This was an oversight on our part.  That said, most applications remove flyouts that are not in-use so as to not taunt users with menus they can't click on.  =)

 

Here's a quick workaround.  Add this to your project and replace your ToolStripMenuItems with this type.

 

Hope this helps! 

 

---

 

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

 

namespace WindowsApplication51 {

    /// <summary>

    /// Workaround for disabled flyout menus still evaluating shortcuts

    /// </summary>

    public class FullyEnabledMenuItem : ToolStripMenuItem {

        protected override bool ProcessCmdKey(ref Message m, Keys keyData) {

 

            if (!IsEveryoneEnabled()) {

                // store off the current state of enabled-ness

                bool isEnabled = Enabled;

                this.Enabled = false;

 

                // process the shortcut

                bool handled = base.ProcessCmdKey(ref m, keyData);

 

                // restore the state of enabledness

                this.Enabled = isEnabled;

 

                // return the result of processcmdkey

                return handled;

            }

            else {

                // everyone was enabled

                return base.ProcessCmdKey(ref m, keyData);

            }

        }

 

        private bool IsEveryoneEnabled() {

            if (!this.Enabled) {

                return false;

            }

 

            // walk up the owning item chain until the top

            ToolStripItem ownerItem = this.OwnerItem;

            while (ownerItem != null) {

                if (!ownerItem.Enabled) {

                    return false;

                }

                ownerItem = ownerItem.OwnerItem;

            }

            return true;

        }

    }

}

 

Posted: Thursday, August 10, 2006 11:56 PM by jfoscoding
Filed under:

Comments

Frans Bouma said:

Thanks! :)
# August 11, 2006 3:15 AM

Frans Bouma said:

Thanks! :) Btw, will this be fixed in the next service pack for .NET 2.0 ?
# August 11, 2006 3:15 AM

jfoscoding said:

I can't remember - I suspect not.  Behavior like this is tough to change in a service pack - it can wind up breaking existing applications who want the behavior.  Again - sorry for the oversight.
# August 11, 2006 11:27 AM

Bonski's Box said:

Frans Bouma (recently) discovered that menuitems inside a flyout/popup menu are still accessible eventhough
# August 15, 2006 8:41 AM
New Comments to this post are disabled
Page view tracker