How to control ToolBar buttons?

Hi,
How to control ie show/hide of ToolBar buttons in Application.not in OnUpdateButton(CCmdUI* pCmdUI)
srikadiAsked:
Who is Participating?
 
ZoppoConnect With a Mentor Commented:
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
 
alb66Commented:
m_pToolBar->GetToolBarCtrl()->HideButton( nID, bHide );
0
 
ZoppoCommented:
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
srikadiAuthor Commented:
sorry,its throwing error. at
m_pToolBar->GetToolBarCtrl().HideButton( nID, bHide );
Access violation error..
 
0
 
alb66Commented:
From wich class you call it?
0
 
srikadiAuthor Commented:
frm mainframe class
0
 
ZoppoCommented:
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
 
alb66Commented:
When you call HideButton() the window is already created?
0
 
srikadiAuthor Commented:
ya..thw window is already created and i called HideButton() under CToolbar click button function.
0
 
alb66Commented:
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
 
ZoppoCommented:
Hi srikadi,

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

ZOPPO
0
 
srikadiAuthor Commented:
hi,
its working now..i tried again in another application..
but its completly hiding the button..what to do to disable the button?
0
 
ZoppoCommented:
Hi,

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

ZOPPO
0
 
srikadiAuthor Commented:
i did with  ON_UPDATE_COMMAND_UI  but is it not possible ither than this?..
0
 
alb66Commented:
m_wndToolBar.GetToolBarCtrl().EnableButton( nID, bEnable );
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.