Microsoft KB Archive/108896

= INFO: Dynamically Disabling/Enabling a Control in a Dialog Box =

Article ID: 108896

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q108896

<div class="notice_section">

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:

<ol>  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; ...               </li>  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. </li>  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); ...      }                </li></ol>

<div class="summary_section">

SUMMARY
Additional query words: kbSweptVC600 kbinf 1.00 1.50 2.00 2.10 2.50 2.51 2.52 3.00 3.10 4.00

Keywords: kbhowto kbuidesign kbdlg kbctrl KB108896

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.