Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Enable/Disable Toolbar *Buttons* :: MFC

Enable/Disable Toolbar *Buttons* :: MFC

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++questionannouncement
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    valikac
    wrote on last edited by
    #1

    Hi. I would like to enable and disable one or more toolbar *buttons*. According to what Prosise mentioned, it can be done using the Enable() that the framework passes to a menu/toolbar update message handler. However, I am not quite clear on how to disable a specific toolbar button. For example, let say I have a toolbar with five buttons. [button1][button2][button3][button4][button5] How would I enable/disable, say, button2? Thanks, Kuphryn

    D 1 Reply Last reply
    0
    • V valikac

      Hi. I would like to enable and disable one or more toolbar *buttons*. According to what Prosise mentioned, it can be done using the Enable() that the framework passes to a menu/toolbar update message handler. However, I am not quite clear on how to disable a specific toolbar button. For example, let say I have a toolbar with five buttons. [button1][button2][button3][button4][button5] How would I enable/disable, say, button2? Thanks, Kuphryn

      D Offline
      D Offline
      Derek Waters
      wrote on last edited by
      #2

      When you create each button in the toolbar, it should have an associated command ID. These IDs are passed in as an array in the CToolBar::SetButtons() command. If you create a toolbar in the resource editor (this is from memory, I don't normally do it this way) I think you just select one of the buttons on your toolbar and bring up the properties, then add a command ID there. Anyway, this ID is the command ID used in the ON_UPDATE_COMMAND_UI update handler (ie you have one handler per toolbar button, not for the toolbar as a whole). If you have a range of toolbar buttons and you don't want to write code for all of them, you can also use ON_UPDATE_COMMAND_UI_RANGE. So, in summary, each Update Command handler should apply to a single toolbar button. ------------------------ Derek Waters derek@lj-oz.com

      V 1 Reply Last reply
      0
      • D Derek Waters

        When you create each button in the toolbar, it should have an associated command ID. These IDs are passed in as an array in the CToolBar::SetButtons() command. If you create a toolbar in the resource editor (this is from memory, I don't normally do it this way) I think you just select one of the buttons on your toolbar and bring up the properties, then add a command ID there. Anyway, this ID is the command ID used in the ON_UPDATE_COMMAND_UI update handler (ie you have one handler per toolbar button, not for the toolbar as a whole). If you have a range of toolbar buttons and you don't want to write code for all of them, you can also use ON_UPDATE_COMMAND_UI_RANGE. So, in summary, each Update Command handler should apply to a single toolbar button. ------------------------ Derek Waters derek@lj-oz.com

        V Offline
        V Offline
        valikac
        wrote on last edited by
        #3

        Okay. Thanks. I am uncertain one something. Here is what I am trying to do. When the application first starts, I want to disable one or more buttons. When the user select a certain option via menu, then the program would enable the button. I need a way to get a hold of a pointer to the toolbar right when the application starts up so I can disable one or more buttons. It can be done with commandUI and commandUI_RANGE, but how about any other arbitrary time? Kuphryn

        D 1 Reply Last reply
        0
        • V valikac

          Okay. Thanks. I am uncertain one something. Here is what I am trying to do. When the application first starts, I want to disable one or more buttons. When the user select a certain option via menu, then the program would enable the button. I need a way to get a hold of a pointer to the toolbar right when the application starts up so I can disable one or more buttons. It can be done with commandUI and commandUI_RANGE, but how about any other arbitrary time? Kuphryn

          D Offline
          D Offline
          Derek Waters
          wrote on last edited by
          #4

          Well, you can put your command handler anywhere that message are received. The best place to put them in this case would probably be the CFrameWnd-derived class where you create the toolbar. The same class would also have a handler for the menu item. For example, if the menu item is ID_ALLOW_BUTTON and the button itself is ID_MY_BUTTON: In the header:

          bool m_bButtonAllowed;

          afx_msg void OnAllowButtonMenu();
          afx_msg void OnUpdateMyButton(CCmdUI *pCmdUI);
          afx_msg void OnMyButton();

          In your constructor:

          m_bButtonAllowed = false;

          In the message map:

          ON_COMMAND(ID_ALLOW_BUTTON, OnAllowButtonMenu)
          ON_UPDATE_COMMAND_UI(ID_MY_BUTTON, OnUpdateMyButton)
          ON_COMMAND(ID_MY_BUTTON, OnMyButton)

          Then:

          CMainFrame::OnAllowButtonMenu()
          {
          m_bButtonAllowed = true;
          }

          CMainFrame::OnUpdateMyButton(CCmdUI *pCmdUI)
          {
          pCmdUI->Enable(m_bButtonAllowed ? 1 : 0);
          }

          CMainFrame::OnMyButton()
          {
          AfxMessageBox("Got me!");
          }

          Hope this helps. ------------------------ Derek Waters derek@lj-oz.com

          V 1 Reply Last reply
          0
          • D Derek Waters

            Well, you can put your command handler anywhere that message are received. The best place to put them in this case would probably be the CFrameWnd-derived class where you create the toolbar. The same class would also have a handler for the menu item. For example, if the menu item is ID_ALLOW_BUTTON and the button itself is ID_MY_BUTTON: In the header:

            bool m_bButtonAllowed;

            afx_msg void OnAllowButtonMenu();
            afx_msg void OnUpdateMyButton(CCmdUI *pCmdUI);
            afx_msg void OnMyButton();

            In your constructor:

            m_bButtonAllowed = false;

            In the message map:

            ON_COMMAND(ID_ALLOW_BUTTON, OnAllowButtonMenu)
            ON_UPDATE_COMMAND_UI(ID_MY_BUTTON, OnUpdateMyButton)
            ON_COMMAND(ID_MY_BUTTON, OnMyButton)

            Then:

            CMainFrame::OnAllowButtonMenu()
            {
            m_bButtonAllowed = true;
            }

            CMainFrame::OnUpdateMyButton(CCmdUI *pCmdUI)
            {
            pCmdUI->Enable(m_bButtonAllowed ? 1 : 0);
            }

            CMainFrame::OnMyButton()
            {
            AfxMessageBox("Got me!");
            }

            Hope this helps. ------------------------ Derek Waters derek@lj-oz.com

            V Offline
            V Offline
            valikac
            wrote on last edited by
            #5

            Thanks. That is exactly what I thought of. Just one more thing: should I map the command UI update in the MainFrm or the view? I am working with doc/view architecture. Kuphryn

            D 1 Reply Last reply
            0
            • V valikac

              Thanks. That is exactly what I thought of. Just one more thing: should I map the command UI update in the MainFrm or the view? I am working with doc/view architecture. Kuphryn

              D Offline
              D Offline
              Derek Waters
              wrote on last edited by
              #6

              Aaah, that old chestnut! It really depends on what the toolbar button does. If the operation of the button could apply to more than one view type (for example, the program I'm working on has ten doc/view types, and some buttons apply to all, while others apply to only one) then it probably makes more sense to put it in MainFrame. If the button only really applies to one view type, it's probably going to be easier to update in the view, otherwise you're going to need to do something like:

              void CMainFrame::MyUpdateHandle(CCMdUI *pCmdUI)
              {
              pCmdUI->Enable(FALSE);
              CFrameWnd *vpFrame = GetActiveFrame();
              if (vpFrame != NULL)
              {
              CView *vpView = vpFrame->GetActiveView();
              if (vpView != NULL)
              {
              if (vpView->IsKindOf(RUNTIME_CLASS(CMyButtonView)))
              {
              CMyButtonView *vpMyView = static_castvpView;
              if (vpMyView->ShouldIEnableTheButton())
              {
              pCmdUI->Enable(TRUE);
              }
              }
              }
              }
              }

              Whew! As opposed to:

              CMyButtonView::OnUpdateButton(CCmdUI *pCmdUI)
              {
              pCmdUI->Enable(ShouldIEnableTheButton());
              }

              So, all in all, it's really a style/ease of programming kind of question. I'll leave that decision up to you... Hope this helps. ------------------------ Derek Waters derek@lj-oz.com

              V 1 Reply Last reply
              0
              • D Derek Waters

                Aaah, that old chestnut! It really depends on what the toolbar button does. If the operation of the button could apply to more than one view type (for example, the program I'm working on has ten doc/view types, and some buttons apply to all, while others apply to only one) then it probably makes more sense to put it in MainFrame. If the button only really applies to one view type, it's probably going to be easier to update in the view, otherwise you're going to need to do something like:

                void CMainFrame::MyUpdateHandle(CCMdUI *pCmdUI)
                {
                pCmdUI->Enable(FALSE);
                CFrameWnd *vpFrame = GetActiveFrame();
                if (vpFrame != NULL)
                {
                CView *vpView = vpFrame->GetActiveView();
                if (vpView != NULL)
                {
                if (vpView->IsKindOf(RUNTIME_CLASS(CMyButtonView)))
                {
                CMyButtonView *vpMyView = static_castvpView;
                if (vpMyView->ShouldIEnableTheButton())
                {
                pCmdUI->Enable(TRUE);
                }
                }
                }
                }
                }

                Whew! As opposed to:

                CMyButtonView::OnUpdateButton(CCmdUI *pCmdUI)
                {
                pCmdUI->Enable(ShouldIEnableTheButton());
                }

                So, all in all, it's really a style/ease of programming kind of question. I'll leave that decision up to you... Hope this helps. ------------------------ Derek Waters derek@lj-oz.com

                V Offline
                V Offline
                valikac
                wrote on last edited by
                #7

                Good point! Thanks. I will stick with the simple code for now since all m views use the same toolbar. Kuphryn

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups