Disable/Enable Menu Items and Sub Items

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2006
Posts: 14
Reputation: vickzbrit is an unknown quantity at this point 
Solved Threads: 0
vickzbrit vickzbrit is offline Offline
Newbie Poster

Disable/Enable Menu Items and Sub Items

 
0
  #1
Nov 21st, 2008
How do I disable and Enable Menu Items and Sub Items.
  1. if(Selected == "drum" || Selected == "guitar" || Selected == "mic" || Selected == "computer" || Selected == "car" || Selected == "hockey" || Selected == "laptop" || Selected == "watch" )
  2. {
  3. pCmdUI->Enable(Sort == 1);
  4. }

The above code used to work before having sub menu items to that menu item. After adding sub menu items, the previous code works for sub menu items but it would not work for menu item. Can anyone please help me out with this as where am i going wrong? Thank you

Vicky
Last edited by vickzbrit; Nov 21st, 2008 at 6:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 35
Reputation: koushal.vv is an unknown quantity at this point 
Solved Threads: 1
koushal.vv koushal.vv is offline Offline
Light Poster

Re: Disable/Enable Menu Items and Sub Items

 
0
  #2
Nov 22nd, 2008
I have faced this problem before....
just go to the menu right click on it ,
say add event handler, Add the ONUpdate
event handler. U will have to enable
Every menu, Sub Menu OnUpdate event handler.
Then its quite simple use the pCMdUI->enable()
method to enable / Disable menu depending upon the condition.
The other method is to create a pop up menu , where the
enabling and disabling of menus will be simpler.
Hope i have solved u r problem , if not reply back here
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 14
Reputation: vickzbrit is an unknown quantity at this point 
Solved Threads: 0
vickzbrit vickzbrit is offline Offline
Newbie Poster

Re: Disable/Enable Menu Items and Sub Items

 
0
  #3
Nov 22nd, 2008
Originally Posted by koushal.vv View Post
I have faced this problem before....
just go to the menu right click on it ,
say add event handler, Add the ONUpdate
event handler. U will have to enable
Every menu, Sub Menu OnUpdate event handler.
Then its quite simple use the pCMdUI->enable()
method to enable / Disable menu depending upon the condition.
The other method is to create a pop up menu , where the
enabling and disabling of menus will be simpler.
Hope i have solved u r problem , if not reply back here
I think I have already done that part and still it would not work. Below is the code to ensure that we are talking about the same part.

  1. void CDeptView::OnSortManager()
  2. {
  3. Selected = "MANAGER"; // Selected is a variable
  4. // Pulling out data from the access table
  5. m_pSet->m_strSort="[Managers].[ManagerName]";
  6. m_pSet->Requery();
  7. m_pSet->MoveFirst();
  8. UpdateData(FALSE);
  9. }

  1. void CDeptView::OnUpdateSortManager(CCmdUI *pCmdUI)
  2. {
  3. // Setting a check to the menu item but it doesn't work now as
  4. it has sub menu items
  5. pCmdUI->SetCheck(( m_SelectedSort == "MANAGER")? 1 : 0 );
  6. // Bellow is the condition when I want the menu item and its
  7. sub item to be disabled.
  8. if(Selected == "drum" || Selected == "guitar" || Selected == "mic" || Selected == "computer" || Selected == "car" || Selected == "hockey" || Selected == "laptop" || Selected == "watch" )
  9. {
  10. pCmdUI->Enable(Sort == 1);
  11. }
  12. }

This is how I have my code and it doesn't work. In a menu bar I have Sort. Under Sort I have Manager. Under Manager I have 2 other menu items. Before having thoes 2 menu items, Manager worked with check mark as well as disabling. After having sub menu items it wont. Can anyone help me out with it.

Vicky
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 35
Reputation: koushal.vv is an unknown quantity at this point 
Solved Threads: 1
koushal.vv koushal.vv is offline Offline
Light Poster

Re: Disable/Enable Menu Items and Sub Items

 
0
  #4
Nov 22nd, 2008
So what u r saying is
first the menu looked like this

SORT
|
MANAGER

Here Enabling and Disabling of manager Menu can be done,
now u have updated the manager menu like this :

SORT
|
MANAGER --- SUBMENU1
--- SUBMENU2

here u will not be able to disable the MANAGER menu as u will not be able to add an Valid event for it. Since u have added the SUNBMENU's now , u can again create the ONUPDATE event for them, then try to put the same logic into them and disable both of the sub menus. Here in the MANAGER menu when u click on it no event will be generated. MANAGER menu will be now same as SORT menu where valid event handler's cant be added. hope this helps u .
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 14
Reputation: vickzbrit is an unknown quantity at this point 
Solved Threads: 0
vickzbrit vickzbrit is offline Offline
Newbie Poster

Re: Disable/Enable Menu Items and Sub Items

 
0
  #5
Nov 22nd, 2008
Originally Posted by koushal.vv View Post
So what u r saying is
first the menu looked like this

SORT
|
MANAGER

Here Enabling and Disabling of manager Menu can be done,
now u have updated the manager menu like this :

SORT
|
MANAGER --- SUBMENU1
--- SUBMENU2

here u will not be able to disable the MANAGER menu as u will not be able to add an Valid event for it. Since u have added the SUNBMENU's now , u can again create the ONUPDATE event for them, then try to put the same logic into them and disable both of the sub menus. Here in the MANAGER menu when u click on it no event will be generated. MANAGER menu will be now same as SORT menu where valid event handler's cant be added. hope this helps u .

Yes, that is correct. But I can disable Sort. The code for that is given below.

  1. void CDept::DisableSort(void)
  2. {
  3. CWnd* pParent = GetParent(); // This is a point to the window
  4. // This is a pointer to the menu on that window
  5. CMenu* pMenu = pParent->GetMenu();
  6.  
  7. if (m_pSet->GetRecordCount()==0) {
  8. MessageBox("Error", MB_ICONSTOP);
  9. // This disables the menu item in the 6th position which
  10. is Sort
  11. pMenu->EnableMenuItem(5 , MF_BYPOSITION | MF_GRAYED );
  12. }
  13. m_pSet->MoveFirst();
  14. pParent->DrawMenuBar(); // To redraw the menu
  15. OnSortEmployee(); // To reset the sort back to the “default condition”
  16. }

But the question is how am i suppose to gray out or disable the menu item in the sort. Check out the code bellow
  1. //(5) is the position of the sort, (MF_BYPOSITION ) is saying that it can be done by position of the Sort which is 5, (MF_GRAYED) means it grays out the SORT.
  2.  
  3. pMenu->EnableMenuItem(5 , MF_BYPOSITION | MF_GRAYED );

Now the problem is I can not do the same thing for the sub menu of Sort. But I don't know the position for that. If there is another piece of code to get the job done, then I have no idea about it. This is where I am stuck.

Vicky
Last edited by vickzbrit; Nov 22nd, 2008 at 2:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 35
Reputation: koushal.vv is an unknown quantity at this point 
Solved Threads: 1
koushal.vv koushal.vv is offline Offline
Light Poster

Re: Disable/Enable Menu Items and Sub Items

 
0
  #6
Nov 22nd, 2008
use the same logic for disabling the MANAGER menu also, Here u r graying it i think this shud also work for MANAGER. i will also try to write a dummy pgm and test this.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 14
Reputation: vickzbrit is an unknown quantity at this point 
Solved Threads: 0
vickzbrit vickzbrit is offline Offline
Newbie Poster

Re: Disable/Enable Menu Items and Sub Items

 
0
  #7
Nov 22nd, 2008
Originally Posted by koushal.vv View Post
use the same logic for disabling the MANAGER menu also, Here u r graying it i think this shud also work for MANAGER. i will also try to write a dummy pgm and test this.
I tried it and it doesn't work for me. There must be a different set of code for that. First thing is I don't know the position of the Manager menu item. Second thing is I tried with the MF_BYCOMMAND (you can put IDC instead of position number) instead of MF_BYPOSITION and it still did not work. Hope to find a solution for this soon :-).

Vicky
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 35
Reputation: koushal.vv is an unknown quantity at this point 
Solved Threads: 1
koushal.vv koushal.vv is offline Offline
Light Poster

Re: Disable/Enable Menu Items and Sub Items

 
0
  #8
Nov 22nd, 2008
Hope this works....

  1. CMainFrame* pFrame ; // Memeber variable
  2. pFrame = (CMainFrame*) AfxGetMainWnd();
  3.  
  4. pFrame->GetMenu()
  5. ->EnableMenuItem(ID_SORT_MANAGER,MF_GRAYED);

But make sure that u have set the m_bAutoMenuEnable = false;
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 14
Reputation: vickzbrit is an unknown quantity at this point 
Solved Threads: 0
vickzbrit vickzbrit is offline Offline
Newbie Poster

Re: Disable/Enable Menu Items and Sub Items

 
0
  #9
Nov 23rd, 2008
Originally Posted by koushal.vv View Post
Hope this works....

  1. CMainFrame* pFrame ; // Memeber variable
  2. pFrame = (CMainFrame*) AfxGetMainWnd();
  3.  
  4. pFrame->GetMenu()
  5. ->EnableMenuItem(ID_SORT_MANAGER,MF_GRAYED);

But make sure that u have set the m_bAutoMenuEnable = false;
The code that you provided here is not working. CMainFrame and pFrame is giving me errors and syntax errors too. Here is what I got right now and I still dont understand why is this not working
  1. CWnd* pParent = GetParent();
  2. CMenu* pMenu = pParent->GetMenu();
  3. CMenu* submenu = pMenu->GetSubMenu(0);
  4. submenu->EnableMenuItem(0, MF_BYPOSITION | MF_GRAYED );

Please help me out
Last edited by vickzbrit; Nov 23rd, 2008 at 4:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Disable/Enable Menu Items and Sub Items

 
0
  #10
Nov 23rd, 2008
Wouldn't it be easiest to add 'standard' ON_UPDATE_COMMAND_UI handlers for those two sub-menus, i.e. you'd have something along the lines of:
  1. ON_UPDATE_COMMAND_UI(IDM_SUBMENU1, OnUpdateSubmenu1)
  2. ON_UPDATE_COMMAND_UI(IDM_SUBMENU2, OnUpdateSubmenu2)
  3. ...
  4. void SomeClass::OnUpdateSubmenu1(CCmdUI* pCmdUI)
  5. {
  6. pCmdUI->Enable( ... )
  7. }
  8. void SomeClass::OnUpdateSubmenu2(CCmdUI* pCmdUI)
  9. {
  10. pCmdUI->Enable( ... )
  11. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC