Share via


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;

        }

    }

}

Comments

  • Anonymous
    August 10, 2006
    Thanks! :)
  • Anonymous
    August 10, 2006
    Thanks! :) Btw, will this be fixed in the next service pack for .NET 2.0 ?
  • Anonymous
    August 11, 2006
    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.
  • Anonymous
    August 15, 2006
    Frans Bouma (recently) discovered that menuitems inside a flyout/popup menu are still accessible eventhough