Dynamically Disabling/Enabling a Control in a Dialog Box

Last reviewed: October 10, 1997
Article ID: Q108896
1.00 1.50 1.51 1.52 | 1.00 2.00 2.10 4.00
WINDOWS             | WINDOWS NT
kbprg

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, and 1.52
        - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, and 4.0
    

This article demonstrates one way to disable or enable a dialog box control during the execution of a program with the Microsoft Foundation Classes (MFC). The dialog box uses a dialog box template created by App Studio.

To disable or enable a control in a dialog box, the following steps can be performed:

  1. Create a data member in the dialog box class that maps to a specific control (you can use Class Wizard to do this). For example:

          ...
          public:
    
          CButton     m_button;
          ...
    
    

  2. Override CWnd::DoDataExchange(CDataExchange *pDX) in the dialog box class and call the appropriate DDX routine to hook the Windows control to the dialog class data member. For example:

          ...
          protected:
          virtual void DoDataExchange(CDataExchange* pDX);
          ...
    

          void CMyDlg::DoDataExchange(CDataExchange* pDX)
          {
    
            CDialog::DoDataExchange(pDX);
            DDX_Control(pDX, IDC_BUTTON1, m_button);
          }
    
       Note: If you use Class Wizard to add a member variable in your dialog
       box class, it does this for you.
    
    

  3. Call CWnd::EnableWindow() to disable or enable the control when needed. For example, you can disable a button when the dialog box is initialized. For example:

          CMyDlg::OnInitDialog()
          {
           ...
           m_button.EnableWindow(FALSE);
           ...
          }
    

    An alternative method is to use the CWnd::GetDlgItem() function to get a CWnd * to the control and then call EnableWindow(). For example:

          CMyDlg::OnInitDialog()
           {
    
            ...
            GetDlgItem(IDC_BUTTON)->EnableWindow(FALSE);
            ...
           }
    


Additional reference words: kbinf 1.00 1.50 2.00 2.10 2.50 2.51 2.52 3.00
3.10 4.00
KBCategory: kbprg
KBSubcategory: MfcUI
Keywords : MfcUI kbprg
Technology : kbMfc
Version : 1.00 1.50 1.51 1.52 | 1.00 2.00
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.