2009-07-21 8 views
0

VC++ 6.0, application MFCCEditBox Problème

Dans CEditBox (IDC_EDIT1) J'ai créé une variable CString (m_strEdit1). Comment puis-je valider que l'entrée est un nombre, y compris le signe (+ et -)? Par exemple: (+10, -56)

Il ne doit pas accepter les caractères alphanumériques. Comment puis-je faire ceci?

Répondre

0
+0

je veux entrer des chiffres non caractères s'il vous plaît me donner l'exemple grâce à avance –

0

Vous devriez probablement faire en validant les personnages comme ils sont entrés:


class CEditBox public CEdit 
{ 
    afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags); 
    DECLARE_MESSAGE_MAP() 
}; 

BEGIN_MESSAGE_MAP(CEditBox, CEdit) 
    ON_WM_CHAR() 
END_MESSAGE_MAP() 

void CEditBox::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{ 
    // quick validation 
    if (nChar != _T('+') && nChar != _T('-') && !isdigit(nChar)) 
     return; 

    CString strText; 
    GetWindowText(strText); 

    int nStartChar; 
    int nEndChar; 
    GetSel(nStartChar, nEndChar); 

    CString strNewText = strText.Left(nStartChar) + 
         nChar + 
         strText.Right(strText.GetLength() - nEndChar); 

    CString strFirst = strNewText.Left(1); 

    // is first character valid? 
    if (strFirst != _T("+") && strFirst != _T("-") && && !isdigit(strFirst[0])) 
     return; 

    CString strSecond = strNewText.Mid(1); 

    // are remaining characters all digits? (ie no more + or -) 
    for (int i = 0; i < strSecond.GetLength(); ++i) 
    { 
     if (!isdigit(strSecond[i])) 
      return; 
    } 

    CEdit::OnChar(nChar, nRepCnt, nFlags); 
}