Visual c++ visualc&x2B+;-MFC如何使用编辑框更改按钮的禁用状态

Visual c++ visualc&x2B+;-MFC如何使用编辑框更改按钮的禁用状态,visual-c++,mfc,Visual C++,Mfc,我目前与MFC合作,我想做一个简单的帐户管理 我做了一个登录按钮,从开始设置为disabled,在2个编辑框中,每个都是一个用户id和一个密码 我想做一件简单的事情:如果其中一个编辑框没有任何值,那么就禁用loggin按钮,否则..使按钮可用 但是,代码根本不起作用 代码如下: 头文件的一部分 // Implementation protected: HICON m_hIcon; // Generated message map functions virtual B

我目前与MFC合作,我想做一个简单的帐户管理

我做了一个登录按钮,从开始设置为disabled,在2个编辑框中,每个都是一个用户id和一个密码

我想做一件简单的事情:如果其中一个编辑框没有任何值,那么就禁用loggin按钮,否则..使按钮可用

但是,代码根本不起作用

代码如下:

头文件的一部分

 // 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();
转到.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);
}

代码有什么问题?

在两个处理程序中,它总是被启用为FALSE。我想你错过了一个
别的


您需要在启用窗口(TRUE)后返回
,或者使用
其他

acraig5057解释了代码的问题,并向您展示了如何解决该问题。我要补充的是,您也可以这样做:将两个编辑控件的EN_更改映射到一个处理程序,我们称之为OnEnChangeEditIdOrPass:

void CTestDlg::OnEnChangeEditIdOrPass()
{        
    m_Btn_Login.EnableWindow((m_CEdit_ID.GetWindowTextLength() != 0) && 
                             (m_CEdit_PASS.GetWindowTextLength() != 0));
}
函数
GetWindowTextLength
返回指定编辑控件中的字符数,如果编辑控件为空,则返回0

上述代码的逻辑是:如果两个编辑框中都有字符,&&将返回
TRUE
,并且将启用登录按钮。如果其中至少有一个没有,则&&将返回
FALSE
,并且登录按钮将被禁用


当然,这段代码不像您的代码那样将用户名和密码编辑控件的值保存到字符串变量中,但您始终可以从登录按钮的处理程序内部调用GetWindowText。

acraig5057,非常感谢。我觉得很惭愧,这个“else”就是问题所在,我一直都知道,当执行if短语时,你可以在没有else的情况下执行一行,因为如果它不起作用,它会跳到下一行。无论如何,这个“其他”解决了我的问题,我会记住的。非常感谢@HarelM要知道,不接受答案会让那个人失去15个代表点,所以一定要确保新的接受是值得的。我不知道。很抱歉当我看到我只能回答一个问题时,我把它还给了你。你是第一个,你应得的,非常感谢!我不知道那件事。