Use ActiveControl to enabled and disable menu items when appropriate in C#

When the user opens a menu, only the commands that should be available at the time should be enabled. This example has Copy, Cut, and Paste menu items. They should be enabled when:

  • Copy - Enabled when the active control is a TextBox and some of its text is selected.
  • Cut - Enabled when the active control is a TextBox and some of its text is selected.
  • Paste - Enabled when the active control is a TextBox and the clipboard contains text.

I have seen programs try to enable and disable menu items in all sorts of ways. Some try to figure out when a condition has changed (such as the user selecting text in a TextBox). Some even use a Timer to enable and disable menus every quarter second.

A much easier solution is to enable and disable menu items when the menu is displayed. This example uses the following code to enable and disable its menu items.

private void EnableMenuItems()
    if (ActiveControl is TextBox)
        TextBox txt = ActiveControl as TextBox;
        mnuEditCopy.Enabled = (txt.SelectionLength > 0);
        mnuEditCut.Enabled = (txt.SelectionLength > 0);
        mnuEditPaste.Enabled = Clipboard.ContainsText();
        // Disable all commands.
        mnuEditCopy.Enabled = false;
        mnuEditCut.Enabled = false;
        mnuEditPaste.Enabled = false;

    ctxCopy.Enabled = mnuEditCopy.Enabled;
    ctxCut.Enabled = mnuEditCut.Enabled;
    ctxPaste.Enabled = mnuEditPaste.Enabled;

This code is reasonably straightforward. It enables and disables the main menu's Copy, Cut, and Paste commands depending on whether the active control is a TextBox, whether text is selected, and whether the clipboard contains text. It then gives the program's context menu commands the same enabled state as the corresponding main menu commands.

So when do you call the EnableMenuItems method? A menu in the form's menu bar raises its Click event when it is opened. The following code calls EnableMenuItems when the Edit menu opens.

// Enable and disable items as appropriate.
private void mnuEdit_Click(object sender, EventArgs e)

This works differently for context menus because the user doesn't actually click a context menu to open it. Instead of using a context menu's Click event, look for its VisibilityChanged event as in the following code.

private void ctxEdit_VisibleChanged(object sender, EventArgs e)

Now when a menu or context menu is displayed, its commands are enabled as appropriate.

Note that this technique doesn't work for toolbar commands or other objects that are always visible. In those cases you need to either leave the item enabled and just have it not do anything when the user invokes it, or detect changes to the rest of the program (like when the user selects text) and set the editability accordingly.



What did you think of this article?

  • No trackbacks exist for this post.
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.


 Email (will not be published)


Your comment is 0 characters limited to 3000 characters.