StackNewbie
StackNewbie

Reputation: 40

Visual C++ - MFC how to change disablity of a button using edit box

I'm currently working with MFC, and I want to make a simple account managment.

I made a login button which is set to disabled from the start and 2 edit box which each one of them is a user id and a password.

I want to make a simple thing : if one of the edit box has no value at all then make the loggin button disabled, else..make the button available.

However, the code doesn't work at all.

this is the code :

part of the header file

 // Implementation
protected:
    HICON m_hIcon;

    // Generated message map functions
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    DECLARE_MESSAGE_MAP()
private:
    // Value of the "Username" textbox
    CString m_CStr_UserID;
    // Control variable of the "Username" textbox
    CEdit m_CEdit_ID;
    // Value of the "Password" textbox
    CString m_CStr_UserPass;
    // Control variable of the "Password" textbox
    CEdit m_CEdit_PASS;
    // Control variable of the "Login" button
    CButton m_Btn_Login;
public:
    afx_msg void OnEnChangeEditId();

    afx_msg void OnEnChangeEditPass();

proceed to the .cpp

 .....
void CTestDlg::OnEnChangeEditId()
{
    // TODO:  If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialog::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO:  Add your control notification handler code here

    m_CEdit_ID.GetWindowTextW(m_CStr_UserID);
    if(!m_CStr_UserID.IsEmpty() && !m_CStr_UserPass.IsEmpty())
        m_Btn_Login.EnableWindow(TRUE);
    m_Btn_Login.EnableWindow(FALSE);
}

void CTestDlg::OnEnChangeEditPass()
{
    // TODO:  If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialog::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO:  Add your control notification handler code here
    m_CEdit_PASS.GetWindowTextW(m_CStr_UserPass);
    if(!m_CStr_UserPass.IsEmpty() && !m_CStr_UserID.IsEmpty())
        m_Btn_Login.EnableWindow(TRUE);
    m_Btn_Login.EnableWindow(FALSE);
}

What's wrong with the code?

Upvotes: 0

Views: 2406

Answers (2)

Nik Bougalis
Nik Bougalis

Reputation: 10613

acraig5057 explained the problem with your code and showed you how to work around it. I will just add that you can also do this: map the EN_CHANGE for both edit controls to one handler, let's call it OnEnChangeEditIdOrPass:

void CTestDlg::OnEnChangeEditIdOrPass()
{        
    m_Btn_Login.EnableWindow((m_CEdit_ID.GetWindowTextLength() != 0) && 
                             (m_CEdit_PASS.GetWindowTextLength() != 0));
}

The function GetWindowTextLength returns the number of characters in the specified edit control and 0 if the edit control is empty.

The logic of the above code is this: if both the edit boxes have characters in them, the && will return TRUE and the login button will be enabled. If at least one of them does not, the && will return FALSE and the login button will be disabled.

Of course, this code not save the values of the username and the password edit controls into string variables, like your code does, but you can always call GetWindowText from inside the handler of the login button.

Upvotes: 0

acraig5075
acraig5075

Reputation: 10756

In both handlers it's always being enabled FALSE. I think you're missing an else

You either need to return after the EnableWindow(TRUE) or use an else.

Upvotes: 1

Related Questions