I have a lot of edit controls on a dialog based form. I need to be able to
restrict the text that is allowed in many of the edit boxes. The valid
characters would be alpha-numeric and underscores, as long as the numbers
and underscores are not the first character. Does someone know how to do
this? I don't want to use any ActiveX controls because I want to package my
application with as few "extra" pieces as possible.
Thanks in advance.
Ryan Taylor
rta...@stgeorgeconsulting.com
http://www.stgeorgeconsulting.com
Ryan,
Derive your own class from CEdit and attach control member variables
to those controls in your dialog.
In your derived class, filter the invalid characters. In this hex
input example, I handle EM_UPDATE because it's a single point where
you can handle the keyboard entry and a clipboard paste operation:
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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.
Ryan Taylor
Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm