Enable\Disable Menu Items

Started by
1 comment, last by Endurion 17 years, 2 months ago
I have a form with a menu on the top. How can I Enable\Disable these items during runtime? For instance how would I enable enable "File-Save" after the user has made a change to a document? Thank you.
Advertisement
You can use the EnableMenuItem function to enable/disable a Menu Item. You'll have to pass it a handle to the menu that owns the item. In case of the File/Save menu entry you'll need a handle to the File sub-menu. Here is a small sample code:
// get a handle to the windows menu barHMENU hMainMenu = GetMenu(hWnd);// get a handle to the file submenu (assuming that File is the first entry in the menu bar)HMENU hSubMenu = GetSubMenu(hMainMenu, 0);// disable save item (assuming it has ID_FILE_SAVE as the menu item id)EnableMenuItem(hSubMenu, ID_FILE_SAVE, MF_BYCOMMAND | MF_DISABLED);

EnableMenuItem either takes the menu item ID (as in my example) or the item's position in the menu. You can choose which one you specify by either using MF_BYCOMMAND (=ID) or MF_BYPOSITION (= zero-based position).

Hope that helps!
Win32: See Above

C#, Windows Forms:

Set the Enabled property of the MenuItem to false.

Your MenuItems will all have a name which you can look up in the properties of the menu items.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement