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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
srikadiAuthor Commented:
sorry,its throwing error. at
m_pToolBar->GetToolBarCtrl().HideButton( nID, bHide );
Access violation error..
 
0
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

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
ZoppoCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.