简体   繁体   中英

Enable/Disable Edit box with help of Check-box control (MFC)

I have a checkbox and an edit control. I want to disable the Edit control when Check-box is 'not checked', and enable Edit control when Check-box is 'checked.

OnBnClickedCheck1 gets called when I check/uncheck the check-box. m_CHECK1_VARIABLE tells me if the check box is checked or un-checked. If-else part is executed correctly but m_TEXT1_CONTROL.EnableWindow(FALSE/TRUE) doesn't seem to work.

Below is the code.

void CPreparationDlg::OnBnClickedCheck1()
{
    UpdateData(TRUE);
    if (m_CHECK1_VARIABLE)
    {
        m_TEXT1_CONTROL.EnableWindow(TRUE);
    }
    else if (m_CHECK1_VARIABLE)
    {
        m_TEXT1_CONTROL.EnableWindow(FALSE);
    }

}

There are 2 cases.

  1. When Edit-box is disabled by default when dialog pops up.

If the Edit-box is enabled by default (I set 'Disabled' behavior in dialog properties to 'False'), Edit-box stays enabled throughout the operation. (check and uncheck operation on check-box)

  1. When Edit-box is enabled by default when dialog pops up.

When I disable the Edit-box by default (I set 'Disabled' behavior in dialog properties to 'True'), Edit-box becomes enabled on 'first' 'check' on the Check-box but stays enabled throughout the rest of the operation. (check and uncheck operation on check-box).

What is it that I am missing here?

The following code example will implement the required logic.

Header file:

public:
    int m_Check;
    CEdit m_EditBox;
    afx_msg void OnBnClickedCheck1();

Class implementation source:

CMfcApplicationDlg::CMfcApplicationDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CMfcApplicationDlg::IDD, pParent)
    , m_Check(0) // Default checkbox state
{
    // ...
}

void CMfcApplicationDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDIT1, m_EditBox);
    DDX_Check(pDX, IDC_CHECK1, m_Check);

    m_EditBox.EnableWindow(m_Check);
}

void CMfcApplicationDlg::OnBnClickedCheck1()
{        
    UpdateData();
}

All required functionality can be implemented inside the DoDataExchange() method. First time the edit box control state set according to the m_Check default value. And each next time the edit box control state will be triggered by OnBnClickedCheck1() event.

IMHO using DoDataExchange(..) to maintain the state of a dialog is dicey at best. Add a member like UdateState( ) and use that. Stage the dialog in OnInitDialog( ) with anything that doesn't easily initialize in the constructor and call UpdateState( ).

Use DoDataExchange(..) only to do what it sounds like, exchange data between the dialog and objects. This way you won't paint yourself into a corner as the Dialog evolves.

//....h
    CEdit m_EditBox;
    CButton m_CheckBox;

//...cpp    
BOOL MyDialog::OnInitDialog( )
{
    if( ! CDialogEx::OnInitDialog( ) )
        return FALSE;
    //do more stuff then
    UpdateState( );
    return TRUE;
}

void MyDialog::UpdateState( )
{
    m_EditBox.EnableWindow( m_CheckBox.GetCheck( ) == BST_CHECKED );
    //more state stuff...
}

void MyDialog::OnBnClickedCheck1( )
{        
    UpdateState( );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM