0

現在MFCを使用しており、簡単なアカウント管理を行いたいと考えています。

最初から無効に設定されているログインボタンと、それぞれがユーザーIDとパスワードである2つの編集ボックスを作成しました。

簡単なことをしたいと思います。編集ボックスの1つに値がまったくない場合は、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);
}

コードの何が問題になっていますか?

4

2 に答える 2

1

どちらのハンドラーでも、常にFALSEが有効になっています。私はあなたが行方不明だと思いますelse

returnEnableWindow(TRUE)の後にするか、を使用する必要がありelseます。

于 2012-11-09T13:17:52.733 に答える
0

acraig5057はコードの問題を説明し、それを回避する方法を示しました。これも実行できることを追加します。両方の編集コントロールのEN_CHANGEを1つのハンドラーにマップし、OnEnChangeEditIdOrPassと呼びましょう。

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

この関数GetWindowTextLengthは、指定された編集コントロールの文字数を返し、編集コントロールが空の場合は0を返します。

上記のコードのロジックは次のとおりです。両方の編集ボックスに文字が含まれている場合、&&が返さTRUEれ、ログインボタンが有効になります。それらの少なくとも1つがそうでない場合、&&は戻りFALSE、ログインボタンは無効になります。

もちろん、このコードは、コードのようにユーザー名とパスワード編集コントロールの値を文字列変数に保存しませんが、ログインボタンのハンドラー内からいつでもGetWindowTextを呼び出すことができます。

于 2012-11-10T00:19:49.530 に答える