How to disable a Submenu runtime

Hi,

I'm working on a VC++ application.
I need to diable a menu item that has a submenu.
Could anyone give me an idea how to do that.

Thanaks in advance.

0
sunil.s51 (3)
1/11/2006 10:21:01 AM
vc.mfc 33608 articles. 0 followers. Follow

2 Replies
566 Views

Similar Articles

[PageSpeed] 25

I'm assuming you have something of the form:

File	Something
	This
	That
	Whatever >  Stuff
		      etc.

and you would like to disable the Whatever item.

This is a bit tricky, particularly in MFC.  I have done this a couple times.  I've used
two different mechanisms.

The first case was in a dialog-based app, where I didn't have ON_UPDATE_COMMAND_UI
capability.  What I did there was to implement the following code in the OnInitPopupMenu
handler.

The trick is you have to use MF_BYPOSITION to do the disabling.  While you might be
tempted to observe that Whatever is the third submenu of the Something menu (and hence has
position index 2), this is the way to get software that breaks easily.  Having spent many
years fixing code that uses MF_BYPOSITION, I have been radicalized, and believe that the
*only* way to use it reliably is if the position is computed at runtime IMMEDIATELY before
the position is used.

While no scheme is perfect, this one is closer to reliable than a compile-time constant.

What I do is a tree walk down the menu tree, looking for an item in the submenu, e.g., I
believe that "Stuff" is always in the submenu (that's the weakness in this scheme, the
presumption of a specific menu item in a specific submenu.  But it is more reliable than
the presumption of a #define being reliable).  

void CSomeClass::OnInitMenuPopup(...parameters here...)
{
 CMenu * menu = GetMenu();   // get top-level menu
 for(int i = 0; i < menu->GetMenuItemCount(); i++)
     { /* scan top level */
       CMenu * submenu = menu->GetSubMenu(i);
       if(SearchAndDisableSubMenu(menu, i, submenu, IDM_STUFF))
           break;
      } /* scan top level */
}

BOOL CSomeClass::SearchAndDisableSubMenu(CMenu * menu, int pos, CMenu * submenu, UINT id)
    {
     for(int i = 0; i < submenu->GetMenuItemCount(); i++)
        { /* search submenu */
         UINT subid = submenu->GetMenuItemID(i);
         if(subid == 0)  
             continue;  // separator
        if(subid == -1)
            { /* submenu */
             CMenu * subsubmenu = submenu->GetSubMenu(i);
             if(SearchAndDisableSubMenu(submenu, i, subsubmenu, id))
                  return TRUE;
             continue;  // submenu
           } /* submenu */
       if(subid != id)
          continue; // not of interest
      // we found the item in this submenu.  Disable the submenu
      menu->EnableMenuItem(pos, MF_BYPOSITION | (condition ? MF_ENALBED : MF_GRAYED));
      // condition represents the test that determines if the item should be enabled
     return TRUE;
    } /* search submenu */
  return FALSE;
  } // CSomeClass::SearchAndDisableSubMenu

(Note that I don't have the actual code available to me, so I'm reconstructing this
on-the-fly.  It is probably pretty close.  I am a strong believer that code should be
written so that incidental and unrelated changes do not break it.  Changing the position
of a menu item without realizing the implications means that a #define cannot be made to
work relilably.  I'd rather write and debug code like this, once, and have the program
work under years of maintenance, than have it suddenly exhibit pathologies after some
trivial change, such as adding or removing a menu item from a menu)

In the case of MFC, I handled this by having an UPDATE_COMMAND_UI handler and doing the
code shown in the OnInitPopupMenu in the update handler.  It seemed to work.  

It has been probably ten years since I did this.
					joe

On 11 Jan 2006 02:21:01 -0800, "Sunil Varma" <sunil.s51@gmail.com> wrote:

>Hi,
>
>I'm working on a VC++ application.
>I need to diable a menu item that has a submenu.
>Could anyone give me an idea how to do that.
>
>Thanaks in advance.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15975)
1/11/2006 2:41:22 PM
I think this is what you want(1st item):

http://www.microsoft.com/msj/0299/c/c0299.aspx

--------
Ajay  Kalra
ajaykalra@yahoo.com

0
ajaykalra (6842)
1/11/2006 2:54:09 PM
Reply:

Similar Artilces:

Disable users=delete mailboxes?
Hello, according to good Microsoft practice I do not delete but simply disable the windows user accounts of employees quitting. Occasionally I still need to access the mailboxes and I noticed that the mailbox folders are now unavailable and I cannot even access them after re-enabling the account. Are the mailbox folders deleted in the same instant you disable a Windows associated user account? If not, how can I access the contents afterwards? /Regards Anders The mailbox is not deleted after an account is deleted. By gr...

How do I disable the start-up virus scan?
Just installed MSOffice 2003. Running WIN XP-Home version. It takes 3.0 minutes for the main office programs (Word, Excel, etc,) to load, then another 3.0 minutes to load each .doc or .xls file. During the file loading process a "RUNNING VIRUS SCAN" msg appears at the bottom of the screen. I want to know how to turn off the automatic virus scan feature. Look at the settings of your virus program -- Regards, Peo Sjoblom (No private emails please, for everyone's benefit keep the discussion in the newsgroup/forum) "Fred Wagner" <Fred Wagner@discussions.mic...

Access to disabled user's mailbox after reenabling
Good morning! I disabled a terminated employee's network account in AD. This morning management wants access to the user's email. Thinking it would be easiest, I changed the user's password and re-enabled the account. However, we still cannot get into the account. I thought perhaps I just needed to wait for the change to propagate but I re-enabled the account 2+ hours ago and our organization is not that big (one Exchange server, 70 users or so). Opening Outlook generates a message saying "Unable to open default mail folders. Permission denied." Trying to open the...

Windows 2003 System Restore Disabled
HI On windows 2003 With R2 Service Pack 2, we have a problem with the following symptoms: - On All servers, In System Properties, there is no system restore Tab - In Disk Cleanup/More options, System restore is Greyed out - Some servers are consuming disk space on a daily basis in the "System Volume information" folder. These files cannot be deleted manually, get "file in use" Thinking that we need to enable system restore in order to access the cleanup functionality, The following has been done: - There was a group policy on the domain to disable system resto...

Did Autorun get disabled?
Autorun no longer works on my Windowns XP Pro/SP3 with the flashdrive I use for my camera and CD/DVD drive. Somewhere in my recollection of Windows security patches I remember something about autorun being disabled. Is there a way to fix this without a registry edit? Using right click/properties/autoplay doesn't do anything with the 2 drives. -- Jeff Installing KB971029 manually will disable Auto/run/ on flash drives but not CD/DVD drives. Jeff Jewitt wrote: > Autorun no longer works on my Windowns XP Pro/SP3 with the flashdrive I > use > for my camera and...

Woorkbook_Open but disable macros
I have written a macro which opens other files. When those other files open, though, I do not want their macros to run (they all have code in Workbook_Open that I would like to ignore). Background: I have a spreadsheet that keeps track of my business expenses for each calendar quarter. Each spreadsheet is made from the same blank spreadsheet which I created as a sort of template. The spreadsheets are al formated with validation and formulas, etc. All have Workbook_Open macros which add an "Expense" menu, with "Add Expense" and "Sort Expense" menu ite...

can you disable spell check on email replies?
Hi, I use Wordmail on Outlook 2000. I have spell check configured to check my emails before they are sent. When I am responding to emails, it checks through not only my response but also the original text. It stops at every email address, every person's name, and any mispellings they have. Is there a way for Outlook to just spell check MY response, and disregard the original text in email replies? Thanks for any help, Amy >Is there a way for Outlook to just spell check >MY response, and disregard the original text in email >replies? Yes. "Tools">"...

sending disabled in OWA2003 with non IE Browsers ? is it true ??
hi folks, i can hardly beleive it: i could not send messages over OWA2003 with netscape 7.1 and mozilla 1.5. is OWA with Exchange 2003 "read-only" ?? thanks ! cheers reini Reini, I believe you will need to use the "Basic" version of Outlook Web Access. Exchange 2003 has a Premium setting and Basic setting. This allows support for down-level clients. You can enable a user to select which type they would like to you when logging on by enabling form based logon. I have included the following article which describes this. 821909 Use Exchange Server 2003 Outlook Web Ac...

GPO needed to stop and disable a service
Hi all, We are running a Windows 2003 domain and I would like to know if anyone can help we with a GPO to *Stop and Disable* the "Help and Support" service? TIA, Clayton On 22/06/2010 14:56, Clayton Sutton wrote: > Hi all, > > We are running a Windows 2003 domain and I would like to know if anyone can > help we with a GPO to *Stop and Disable* the "Help and Support" service? Sure: Create a new GPO (or use an existing one if applicable to the computers you're wanting to apply it to), and navigate to Computer Configuration -...

Disable share workbook option in VBA
I've got a workbook with a lot of code and want to prevent users from sharing the workbook. If they share the workbook, it only generates error messages for them and creates problems. If I could hide the command bar option or catch when they go to share the workbook and cancel the action, that would be great. But I searched high and low on how to do this with no luck. Users will be using XL 2002 up to 2007. Does anyone have any ideas or faced this challenge before? Les Thanks John, That works like a charm in XL 2003. It would be a treat if I can also stop XL 2007 users fr...

disabling the printer function
Hello, Is it possible to disable the printer on a certain spreadsheet in excel (like how you can password protect, etc...). I want people to be able to open a spreadsheet and look at it, but not be able to modify or print it. Thanks, Jens use the Before_print event to exit or whatever you want -- Don Guillett SalesAid Software Granite Shoals, TX donaldb@281.com "Jens Letnes" <jens.letnes@twosicilys.com> wrote in message news:01df01c34d5f$b681af10$a401280a@phx.gbl... > Hello, > Is it possible to disable the printer on a certain > spreadsheet in excel (lik...

Runtime error while using excel help
I get the below error message "A Runtime Error has occured. Do you wish to debug? Line: 0 Error: Object doesn't support this property or method" This message comes up when I'm using Excel 2002 help. However, the message only comes up when I click on the See Also link. To see this link, type in "LEFT worksheet function" under type keywords in help and click search. Any help would be appreciated in trying to solve this. ...

how do i disable "running virus scan" in excel program when opened
When I open my file in excel it continues to say running virus scan for hours and overnite. How do I stop this so I can use this file? It's not excel, it's your anti-virus program, check the settings there Regards, Peo Sjoblom "Tammy" wrote: > When I open my file in excel it continues to say running virus scan for hours > and overnite. How do I stop this so I can use this file? ...

System.Runtime.InteropServices.COMException error in MSCRM
I am getting this error when I open an existing sales order in crm. I am new to CRM and this error looks weird and doesnt give any information what happened wrong. Can anyone help me?? Microsoft CRM Unhandled Error Details: Server Error in '/' Application. -------------------------------------------------------------------------------- Unhandled Execution Error Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.R...

Runtime error '3251': Operation is not supported for this type of object
I've finally beaten this passthrough query into submission, but when I try to use it as the table argument in a TransferSpreadsheet procedure in VBA, I get the subject error msg. Here's the query: SELECT Full_Name, ISNULL(Op_Com,'"') OpCom, ISNULL(Sub_Op_Comm, '"') SubOpCom FROM dbo.v_Storage_Qtree_App WHERE created='3/9/2008' AND tier_type Like '%Tier 1%' When I run it in Access, it delivers exactly what I'm looking for. Any suggestions would be deeply appreciated. Thanks, -- Bill Reed "If you can't laugh at yourself, laugh ...

Enabling/Disabling menus in dialog
Hi, I facing some problems in enabling/disabling menu items in a dialog. Though I understand that the MFC framework does not directly support handling ON_UPDATE_COMMAND_UI for dialog menus, is there a way to invoke the same? I've looked through some samples on Codeguru, but did not find anything useful on enabling/disabling menus with sub-menus in them. Appreciate any help. Thanks, Sucharit "Sucharit Dutta" <anonymous@discussions.microsoft.com> wrote in message news:D6EDF3F3-429E-47DA-83A6-2C580E7BBDC5@microsoft.com... > Hi, > I facing some problems in enablin...

junk mail maintains disabling when moved to Inbox
Recently when I select an email from the Junk Mail folder, mark it as not junk and allow the sender as a safe sender and then move the email to my Inbox, the email is still showing this dialogue "Links and other functionality have been disabled in this message. To restore functionality, move this message to the Inbox. This message was marked as spam unsing the Outlook Junk E-mail filter." It used to be that when I did all the steps and moved it to my Inbox, the message was like any accepted message, but this way I can't reply or move it anywhere else. If you are...

Question on disabling WLM 2009 parts
Ok, I am using WLM2009 now also .. and not much to my surprise all kinds of things are there that are totally useless for me. Like Windows Live Call ... I do not want that, yet I can not uninstall it cause it HAS to be a part of WLM2009. It also now sits in my RightClick/New part in Windows. How can I stop/uninstall Windows Live Call? Another thing (even more irritating for me): wlcomm.exe. Seems that is part of the Windows Live Contacs service, so I can see directly when my contacts change something on their Live Space or whatever. I do not want that either: when they have n...

Disable Date localization
Is there a way to prevent certain date attributes from localizing based on time zone? I am storing usage statistics and want to display the dates to our users in the time that they occurred instead of being converted to the time they occurred in the user's time zone. I can accomplish this in reports using filtered views, but want to be able to display the correct date on forms as well. ...

Shift key to disable macros not working
I have a macro that runs upon opening a file. I believe that holding down the shift key is supposed to disable automatic macros. It is not disabling. Any suggestions? -- David P. Try to open Excel in Safe Mode (Uisng /s) -- Gary's Student "David P." wrote: > I have a macro that runs upon opening a file. I believe that holding down the > shift key is supposed to disable automatic macros. It is not disabling. Any > suggestions? > -- > David P. You might try setting your macro security to Medium. -- Jim "David P." <DavidP@discussions.microso...

Runtime Error R6025
The last updates I installed were on 6/17 and they were kb978695, 975562, 979482. I didn't get an error until today so I don't know if this is related. All of a sudden I am getting Runtime error! Program: C:\Program files\Internet Explorer\iexplore.exe, R6025 - pure virtual function call. Once you click ok the browser closes. It happened at a couple of bank sites. I've googled for a fix and saw lots of people have had this problem but I didn't find a solution. The only other thing I changed recently was that I replaced a CD-RW drive yesterday. But I did...

Secure form disables spell check
I created a survey by using the "form" features in Word. When I locked it so others can fill in the form, it disabled spell check. How do I enable it? -- Karen See http://www.word.mvps.org/FAQs/MacrosVBA/SpellcheckProtectDoc.htm -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit. Roachie wrote: > I created a survey by using the "form" features in Word. When I > locked it so others can fill in the form, it disabled spell check....

How to disable AutoPlay programmatically
Hello, I have an VC++ 6.0 MFC application as a setup program that requires the user to insert another CDs, in order to copy them before installation, and on some Windows versions like 2003 SP1, the shell launches the Windows Explorer with the CD-ROM logical unit. Also when the application CD is required after the copy, the autorun.inf executes a welcome HTML page. My application also uses WM_DEVICECHANGE messages in order to detect CD inserts, but I want that DBT_DEVICEARRIVAL events only be processed by my installation program. I've tried to send a broadcast WM_DEVICECHANGE message ...

How to disable these frequent IntExp 8 crash popups ?
I upgraded recently from IntExp 7 to IntExp 8 (under WinXP + SP3). When I use now IntExp8 then frequently (5-10 times/day) I got crash notification popups from IntExp. The following snapshot show a sample: http://img443.imageshack.us/img443/7184/intexp8crash.png When I click "Close" then IntExp continues to works as if nothing happened. How can I permanently disable these popups? Brian "Brian Cooper" <bcoo_x@download.net> wrote in message news:4c1dbaec$0$6887$9b4e6d93@newsspool2.arcor-online.net... > I upgraded recently from IntExp 7 to I...

Temporarily uninstalling or disabling printers
A user reports that my MFC app crashes if she tries to print without any printers being installed. I'd like to reproduce that problem here, but I have several printers installed and would prefer not to have to uninstall and then reinstall all of them, possibly losing any custom configuration or other settings I may have. Is there a reasonably easy way to temporarily disable printers so I can reproduce this problem, or is there another way I can simulate the "no printers installed" case? Thanks in advance for any help. Jeff Garbers Hi Jeff, >Is there a reasonably easy way t...