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. Disabling dialog button from within the ON_BN_CLICKED handler

Disabling dialog button from within the ON_BN_CLICKED handler

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestion
8 Posts 5 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.
  • D Offline
    D Offline
    Daniele Godi
    wrote on last edited by
    #1

    Hi all, I have a problem. I need to start a long computing work when the user clicks a dialog button. During this time the button needs to be disabled. The following simple code doesn't work. Despite the fact that the button changes state from enable to disabled (grayed), it actually gets the mouse inputs. Some of you knows the reason? I wasn't able to disable this button and then I had to write a workaround. Thanks, Daniele Godi /////////////////////////////////////////////////////////////// Sample code void CMyDlg::OnBnClickedButton1() { // disable the button m_ctrlButton1.EnableWindow(FALSE); BeginWaitCursor(); DoSomeLongWork(); EndWaitCursor(); // workaround, I have to remove manually the mouse input from // the message queue if (::GetInputState()) { MSG msg; while ( ::PeekMessage( &msg, NULL, NULL, NULL, PM_NOREMOVE ) ) { // found a mouse message, remove it if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST)) { ::PeekMessage( &msg, NULL, NULL, NULL, PM_REMOVE); } else { if ( !::AfxGetThread()->PumpMessage() ) { // if FALSE exit both dialog and application EndDialog(FALSE); ::PostQuitMessage( -1 ); return; } } } } m_ctrlButton1.EnableWindow(TRUE); return; } Daniele Godi

    T U 2 Replies Last reply
    0
    • D Daniele Godi

      Hi all, I have a problem. I need to start a long computing work when the user clicks a dialog button. During this time the button needs to be disabled. The following simple code doesn't work. Despite the fact that the button changes state from enable to disabled (grayed), it actually gets the mouse inputs. Some of you knows the reason? I wasn't able to disable this button and then I had to write a workaround. Thanks, Daniele Godi /////////////////////////////////////////////////////////////// Sample code void CMyDlg::OnBnClickedButton1() { // disable the button m_ctrlButton1.EnableWindow(FALSE); BeginWaitCursor(); DoSomeLongWork(); EndWaitCursor(); // workaround, I have to remove manually the mouse input from // the message queue if (::GetInputState()) { MSG msg; while ( ::PeekMessage( &msg, NULL, NULL, NULL, PM_NOREMOVE ) ) { // found a mouse message, remove it if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST)) { ::PeekMessage( &msg, NULL, NULL, NULL, PM_REMOVE); } else { if ( !::AfxGetThread()->PumpMessage() ) { // if FALSE exit both dialog and application EndDialog(FALSE); ::PostQuitMessage( -1 ); return; } } } } m_ctrlButton1.EnableWindow(TRUE); return; } Daniele Godi

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      if you apply the EnableWindow() directly to a member object, you have to call UpdateData(FALSE) to update your controls with the changes done on the associated member object (and UpdateData(TRUE) for the opposite)... otherwise, this single line also works :

      ((CButton*)GetDlgItem(IDC_MY_BUTTON))->EnableWindow(FALSE); // To disable

      I assume (of course) that IDC_MY_BUTTON is the ident of the button you want to change the state...


      TOXCCT >>> GEII power
      [toxcct][VisualCalc]

      Q 1 Reply Last reply
      0
      • D Daniele Godi

        Hi all, I have a problem. I need to start a long computing work when the user clicks a dialog button. During this time the button needs to be disabled. The following simple code doesn't work. Despite the fact that the button changes state from enable to disabled (grayed), it actually gets the mouse inputs. Some of you knows the reason? I wasn't able to disable this button and then I had to write a workaround. Thanks, Daniele Godi /////////////////////////////////////////////////////////////// Sample code void CMyDlg::OnBnClickedButton1() { // disable the button m_ctrlButton1.EnableWindow(FALSE); BeginWaitCursor(); DoSomeLongWork(); EndWaitCursor(); // workaround, I have to remove manually the mouse input from // the message queue if (::GetInputState()) { MSG msg; while ( ::PeekMessage( &msg, NULL, NULL, NULL, PM_NOREMOVE ) ) { // found a mouse message, remove it if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST)) { ::PeekMessage( &msg, NULL, NULL, NULL, PM_REMOVE); } else { if ( !::AfxGetThread()->PumpMessage() ) { // if FALSE exit both dialog and application EndDialog(FALSE); ::PostQuitMessage( -1 ); return; } } } } m_ctrlButton1.EnableWindow(TRUE); return; } Daniele Godi

        U Offline
        U Offline
        User 527726
        wrote on last edited by
        #3

        Hi, why don't u check LockWindowUpdate() and unLockWindowUpdate(). :cool: Lokesh

        Q 1 Reply Last reply
        0
        • U User 527726

          Hi, why don't u check LockWindowUpdate() and unLockWindowUpdate(). :cool: Lokesh

          Q Offline
          Q Offline
          qrverona
          wrote on last edited by
          #4

          I'm sorry, it desn't work. Ciao... Daniele

          1 Reply Last reply
          0
          • T toxcct

            if you apply the EnableWindow() directly to a member object, you have to call UpdateData(FALSE) to update your controls with the changes done on the associated member object (and UpdateData(TRUE) for the opposite)... otherwise, this single line also works :

            ((CButton*)GetDlgItem(IDC_MY_BUTTON))->EnableWindow(FALSE); // To disable

            I assume (of course) that IDC_MY_BUTTON is the ident of the button you want to change the state...


            TOXCCT >>> GEII power
            [toxcct][VisualCalc]

            Q Offline
            Q Offline
            qrverona
            wrote on last edited by
            #5

            I'm sorry, it desn't work. Ciao... Daniele

            T 1 Reply Last reply
            0
            • Q qrverona

              I'm sorry, it desn't work. Ciao... Daniele

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              what's your code ?


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              L 1 Reply Last reply
              0
              • T toxcct

                what's your code ?


                TOXCCT >>> GEII power
                [toxcct][VisualCalc]

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                The code is exactly the same I have provided on my first query. (Except for the workaround) I work with MS Visual Studio .NET 2003 The application is MFC based. The Dlg-box is modal Build a empty application and add an handler to the OK button on the about box, then 1 - at the beginnig of the handling function disable the button that the user pressed using the wrapper class m_ctrlButtonOK.EnableWindow(FALSE) (same as GetDlgItem(ID)->EnableWindow(FALSE)) 2 - perform a long work ::MessageBeep(MB_OK); ::Sleep(10000); works fine... 3 - at the end of the function call m_ctrlButtonOK.EnableWindow(TRUE) If you click on the OK button while it is disabled, you will see that after the work the handler function get called once again. Thank you and ciao... Daniele

                D 1 Reply Last reply
                0
                • L Lost User

                  The code is exactly the same I have provided on my first query. (Except for the workaround) I work with MS Visual Studio .NET 2003 The application is MFC based. The Dlg-box is modal Build a empty application and add an handler to the OK button on the about box, then 1 - at the beginnig of the handling function disable the button that the user pressed using the wrapper class m_ctrlButtonOK.EnableWindow(FALSE) (same as GetDlgItem(ID)->EnableWindow(FALSE)) 2 - perform a long work ::MessageBeep(MB_OK); ::Sleep(10000); works fine... 3 - at the end of the function call m_ctrlButtonOK.EnableWindow(TRUE) If you click on the OK button while it is disabled, you will see that after the work the handler function get called once again. Thank you and ciao... Daniele

                  D Offline
                  D Offline
                  Daniele Godi
                  wrote on last edited by
                  #8

                  Dear all, I found the solution and I like to share it with you. I took the idea from an article I have found on your site, http://www.codeproject.com/cpp/cppforumfaq.asp#ui\_workerthread I implemented this code within the button handler and... wonder!, it works. OnButtonClicked() { m_crtlButton->Enable(FALSE); while (Do_A_Bit_Of_Work()) { MSG msk; while(PeekMessage(&msg, NULL, NULL, PM_NOREMOVE) { AfxGetApp()->PumpMessage(); } } m_crtlButton->Enable(TRUE); return; } Ciao and thanks for your kind help. Daniele

                  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