Re: Simple Edit Box Validation???
I need to create some simple TextBox validation. I will need
to limit the length to a fixed number of characters.
CEdit::SetLimitText (EM_SETLIMITTEXT)
I also
need to restrict the data entered two different ways:
(a) Numeric Only
(b) AlphaBetic or Numeric Only (hexadecimal numbers), with
conversion to uppercase.
Here's an example of a derived edit control class that handles the
EN_UPDATE notification message. The EN_UPDATE message is useful to
handle as it caters for both normal entry and clipboard paste
operations.
void CHexEdit::OnUpdate()
{
CString str;
GetWindowText( str );
/* Access the string buffer directly */
LPSTR pBuff = str.GetBuffer( 10 );
bool bProblem = false;
for ( int indx = 0; indx < str.GetLength(); indx++ )
{
char nChar = pBuff[indx];
if ( ( ( nChar >= '0' ) && ( nChar <= '9') ) ||
( ( nChar >= 'A' ) && ( nChar <= 'F' ) ) ||
( ( nChar >= 'a' ) && ( nChar <= 'f' ) ) )
{
}
else
{
bProblem = true;
break;
}
}
str.ReleaseBuffer();
if ( bProblem )
{
int start, end;
/* Find the current caret position */
GetSel( start, end );
/* Restore the last good text that was entered */
SetWindowText( m_LastGood );
/* Restore the caret */
SetSel( start-1, end-1, true );
/* Let the user know */
MessageBeep( MB_OK );
}
else
{
/* Store the last good entry string in a
* member variable of the Hex edit class
*/
m_LastGood = str;
}
}
Dave