Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

restrict characters in a CEdit control

419 views
Skip to first unread message

Ryan Taylor

unread,
Jul 25, 2002, 2:29:39 PM7/25/02
to
Hello.

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


David Lowndes

unread,
Jul 25, 2002, 2:43:04 PM7/25/02
to
>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.

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

unread,
Jul 25, 2002, 4:06:27 PM7/25/02
to
Thanks for the reply. This sovled my problem. Now my app will be much more
robust.

Ryan Taylor

Joseph M. Newcomer

unread,
Jul 27, 2002, 10:22:17 PM7/27/02
to
Take a look at my validating edit control on my MVP Tips site. In this control, I restrict
the user to typing only syntactically valid floating point numbers, but you can apply the
principles to any other form of input you can describe easily.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

0 new messages