Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Command Bars
  New Posts New Posts RSS Feed - Main menu item enabling/disabling
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Main menu item enabling/disabling

 Post Reply Post Reply
Author
Message
sunilwarke View Drop Down
Newbie
Newbie


Joined: 28 September 2006
Status: Offline
Points: 6
Post Options Post Options   Thanks (0) Thanks(0)   Quote sunilwarke Quote  Post ReplyReply Direct Link To This Post Topic: Main menu item enabling/disabling
    Posted: 29 November 2006 at 5:46am
Hi,
 
I have the following code, I want to enable and disable some menu buttons on creating MDI application. But wherever I call GetMenu, it always return NULL and the program crashes.
 
Can anybody tell me how to enable or disable the Main Menu controls when using CXTPCommandBars?
 
I tried several options to get the control on the Menu buttons but could not succeeded. If anybody knows how to do it especially in MDI application then please let me know.
 
Here is the code
-----------------------------------------------------------------------------------------
// Get a pointer to the command bars object.
 CXTPCommandBars* pCommandBars = GetCommandBars();
 if(pCommandBars == NULL)
 {
  TRACE0("Failed to create command bars object.\n");
  return -1;      // fail to create
 }
 // Add the menu bar
 CXTPCommandBar* pMenuBar = pCommandBars->SetMenu(
  _T("TIS Menu Bar"), IDR_MDI_TYPE);
 if(pMenuBar == NULL)
 {
  TRACE0("Failed to create menu bar.\n");
  return -1;      // fail to create
 }
 // System popup menu
 pMenuBar->SetFlags(xtpFlagAddMDISysPopup);
 // Create ToolBar
 CXTPToolBar* pToolBar = (CXTPToolBar*)
  pCommandBars->Add(_T("Standard"), xtpBarTop);
 if (!pToolBar || !pToolBar->LoadToolBar(IDR_MDI_MAIN))
 {
  TRACE0("Failed to create toolbar\n");
  return -1;
 }
 
CMenu *mymenu = pMenuBar->GetMenu();
//CMenu *mymenu = GetMenu();

 CString temp, temp1 = "";
 if(mymenu->GetMenuItemCount() > 0)
 {
  for(int j = 0; j < mymenu->GetMenuItemCount(); j++)
  {
   mymenu->GetMenuStringA(j, temp, MF_BYPOSITION);
   temp1.Append(temp);
  }

  ::MessageBox(NULL, temp1, "Count ok", MB_OK);

 }
-----------------------------------------------------------------------------------------

TR,
Sunil
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 29 November 2006 at 6:09am
Hello,
 
Use Update handlers. See any sample from CommandBars folder.
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
sunilwarke View Drop Down
Newbie
Newbie


Joined: 28 September 2006
Status: Offline
Points: 6
Post Options Post Options   Thanks (0) Thanks(0)   Quote sunilwarke Quote  Post ReplyReply Direct Link To This Post Posted: 29 November 2006 at 6:42am
Dear Mr. Oleg
 
Thank you for your reply. I have looked at the CommandBar folder for the similar sample code but I did not find the specific one.
 
In my application, there are 4 child windows and the Menu enabling and disabling is not depend on the child selection or creation. The menu items shall be enabled or disabled on some event (coming from database). So I have written a function which shall enable or disable menu items based on the event.
 
On creation some controls will be enabled and some of them shall be disabled.
 
Can you point out specific example for me? Or can you please send me a sample code like a function.
 
void EnableMenuItem(True)
{
    GetMenu()
    EnableItem(Id, enable);
    Update();
}
 
My problem is to get the pointer to the Menu so that I can enable/disable the item based on the Id.
 
 
TR,
Sunil
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 30 November 2006 at 1:52am
Here steps:
 
Add member variable for CMainFrame m_bDatabaseCommandEnabled;
 
Change EnableMenuItem to:
 
void CMainFrame::EnableMenuItem(BOOL bEanbled)
{
    m_bDatabaseCommandEnabled = bEanbled;
}
 
add handlers for all commands you need:
 
 
ON_UPDATE_COMMAND_UI(IDC_TOOLBARCOMMAND, OnUpdateToolbarCommand)
...
void CMainFrame::OnUpdateToolbarCommand(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bDatabaseCommandEnabled );
}
 
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
sunilwarke View Drop Down
Newbie
Newbie


Joined: 28 September 2006
Status: Offline
Points: 6
Post Options Post Options   Thanks (0) Thanks(0)   Quote sunilwarke Quote  Post ReplyReply Direct Link To This Post Posted: 30 November 2006 at 5:02am
Dear Oleg,
 
Thank you very much. I have solved my problem using the solution you gave me.  I have learnt more about ON_UPDATE_COMMAND_UI which was new to me.
 
Once again thank you.
 
TR,
Sunil Warke
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.109 seconds.