[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to control ToolBar buttons?

Posted on 2008-11-06
16
Medium Priority
?
367 Views
Last Modified: 2013-11-20
Hi,
How to control ie show/hide of ToolBar buttons in Application.not in OnUpdateButton(CCmdUI* pCmdUI)
0
Comment
Question by:srikadi
  • 5
  • 5
  • 5
15 Comments
 
LVL 19

Expert Comment

by:alb66
ID: 22893834
m_pToolBar->GetToolBarCtrl()->HideButton( nID, bHide );
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 22893852
nearly, just one error - it has to be:

> m_pToolBar->GetToolBarCtrl().HideButton( nID, bHide );

since CToolBar::GetToolBarCtrl() returns a reference, not a pointer ...

ZOPPO
0
 

Author Comment

by:srikadi
ID: 22893907
sorry,its throwing error. at
m_pToolBar->GetToolBarCtrl().HideButton( nID, bHide );
Access violation error..
 
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 19

Expert Comment

by:alb66
ID: 22893913
From wich class you call it?
0
 

Author Comment

by:srikadi
ID: 22893927
frm mainframe class
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 22893934
This was just a sample using a pointer 'm_pToolBar'.

In general a CMainFrame's toolbar is a 'CToolBar m_wndToolBar', so the code would be:

> m_wndToolBar.GetToolBarCtrl().HideButton( ... );

ZOPPO
0
 
LVL 19

Expert Comment

by:alb66
ID: 22893936
When you call HideButton() the window is already created?
0
 

Author Comment

by:srikadi
ID: 22893944
ya..thw window is already created and i called HideButton() under CToolbar click button function.
0
 
LVL 19

Expert Comment

by:alb66
ID: 22893979
mmh, very strange...
I tried it with a new program and it works.
As Zoppo show to you, with a defualt application you must write

m_wndToolBar.GetToolBarCtrl().HideButton( ... );
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 22894000
Hi srikadi,

maybe you could simply post the code of the function ...

ZOPPO
0
 

Author Comment

by:srikadi
ID: 22894057
hi,
its working now..i tried again in another application..
but its completly hiding the button..what to do to disable the button?
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 22894117
Hi,

to disable it you need to implement an ON_UPDATE_COMMAND_UI handler for the button's command ID.

ZOPPO
0
 

Author Comment

by:srikadi
ID: 22894138
i did with  ON_UPDATE_COMMAND_UI  but is it not possible ither than this?..
0
 
LVL 19

Expert Comment

by:alb66
ID: 22894287
m_wndToolBar.GetToolBarCtrl().EnableButton( nID, bEnable );
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 2000 total points
ID: 22894476
Hi,

alb66's solution IMO won't work, since the next CWinThread::OnIdle will send a new WM_IDLEUPDATECMDUI is sent so ON_UPDATE_COMMAND_UI handlers are invoked - if none exists for the button it's always enabled if a ON_COMMAND handler exists for the button, otherwise disabled.

It can be done, but fro the whole toolbar, not per button. To do this derive a class from CToolBar like this:

> class CMyToolBar : public CToolBar
> {
> public:
>  virtual void OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler);
> };

Replace the 'CToolBar m_wndToolBar' in mainfrm.h with this:

> CMyToolBar m_wndToolBar;

Implement CMyToolBar's 'OnUpdateCmdUI' like this:

/*virtual*/ void
CMyToolBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
{
      CToolBar::OnUpdateCmdUI(pTarget, FALSE);
}


If you did so you can implement button handling like this (in this sample there are two buttons 'ID_BUTTON_1' and 'ID_BUTTON_2'. Button1 toggles the enabled state of Button2.

> void CMainFrame::OnButton1()
> {
>  CToolBarCtrl& ctrl = m_wndToolBar.GetToolBarCtrl();
>  ctrl.EnableButton( ID_BUTTON_2, ctrl.IsButtonEnabled( ID_BUTTON_2 ) == FALSE );
>  }

> void CMainFrame::OnButton2()
> {
>  AfxMessageBox( "Test" );
> }


With this you can handle enabled-state for button2 directly as long as there's no ON_UPDATE_COMMAND_HANDLER for button2 which changes enabled-state.

Hope that helps,

ZOPPO

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question