How to filter lower case characters in an edit control?  
Author Message
Fati





PostPosted: Sat Oct 21 13:41:43 CDT 2006 Top

MFC >> How to filter lower case characters in an edit control? Hi to all,

I created a class that enherits from CEdit and overrode its OnChar() message
handler to intercept the entered keys and filter only those that are lower
case. It also verifies if an entered key is upper case to convert it to lower
case and filter it.

Everything works good except the conversion from upper to lower case. The
problem is that even if the OnChar() handler does convert the character to
lower case, the character is still displayed in the edit box as it was
entered, i.e. in upper case.

Please note that if I remove the line: "nChar = tolower(nChar);", the upper
case characters will be rejected and this is not what I want. I want the same
behaviour than the edit control's Lowercase property, which will convert all
characters to lowercase as they are typed into the edit box. However, this
property allows all other character types to be output.

Here's the code of the OnChar() message handler:

#define BACK_SPACE 8

void CEditLoCase::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
TRACE("OnChar(): %c ; %d\n", nChar, nFlags);

// Filter all the characters in 'm_pFilter' to be processed.
nChar = tolower(nChar);
if ((nChar >= 'a' && nChar <= 'z') ||
nChar == BACK_SPACE)
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
else
{
MessageBeep(MB_OK);
}
}


I appreciate any help and thank you in advance.

Geo

Visual Studio176  
 
 
David





PostPosted: Sat Oct 21 13:41:43 CDT 2006 Top

MFC >> How to filter lower case characters in an edit control? >I created a class that enherits from CEdit and overrode its OnChar() message
>handler to intercept the entered keys and filter only those that are lower
>case. It also verifies if an entered key is upper case to convert it to lower
>case and filter it.

Geo,

Why not just create the control with the ES_LOWERCASE style?

Dave
 
 
Norbert





PostPosted: Sun Oct 22 04:22:00 CDT 2006 Top

MFC >> How to filter lower case characters in an edit control? Geo schrieb:

> void CEditLoCase::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
> {
[...]
> CEdit::OnChar(nChar, nRepCnt, nFlags);

To "hook" into the default behaviour of a Non-MFC windows control, you need
"Window"-subclassing instead of "C++ MFC" subclassing. If you step into the
CEdit::OnChar function above with your debugger, you will notice that the
handler will probably do nothing (I have not checked), at least it will not
forward the message to the windows edit control.

Have a look at CWnd::SubclassWindow.

Norbert
 
 
Joseph





PostPosted: Sun Oct 22 11:42:44 CDT 2006 Top

MFC >> How to filter lower case characters in an edit control? This problem arises because Microsoft totally screwed up the MFC implementation on day 1.
You can't change ANY of the parameters that are coming in; the superclass call simply
takes the incoming parameters and reuses them, instead of doing something intelligent,
such as using your parameters! (That paragraph at the end of every MFC call is there
because I collarred the MFC guys at an MVP conference and told them they had to fix MFC to
be right. They didn't, but they at least notified us that every call is screwed up.
Apparently they never *actually* understood object-oriented programming and subclassing,
and kludged up something that sort-of-worked most of the time.)

However, as already pointed out, using the ES_LOWERCASE flag will solve your problem, so
your approach is needlessly complicated. You don't even need subclassing to implement it,
just a ModifyStyle call (ES_LOWERCASE and ES_UPPERCASE are two style bits that can be
freely modified after the control is created)
joe

On Sat, 21 Oct 2006 06:10:02 -0700, Geo <EMail@HideDomain.com> wrote:

>Hi to all,
>
>I created a class that enherits from CEdit and overrode its OnChar() message
>handler to intercept the entered keys and filter only those that are lower
>case. It also verifies if an entered key is upper case to convert it to lower
>case and filter it.
>
>Everything works good except the conversion from upper to lower case. The
>problem is that even if the OnChar() handler does convert the character to
>lower case, the character is still displayed in the edit box as it was
>entered, i.e. in upper case.
>
>Please note that if I remove the line: "nChar = tolower(nChar);", the upper
>case characters will be rejected and this is not what I want. I want the same
>behaviour than the edit control's Lowercase property, which will convert all
>characters to lowercase as they are typed into the edit box. However, this
>property allows all other character types to be output.
>
>Here's the code of the OnChar() message handler:
>
>#define BACK_SPACE 8
****
Why 8? Why not _T('\b')?
****
>
>void CEditLoCase::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
>{
> TRACE("OnChar(): %c ; %d\n", nChar, nFlags);
>
> // Filter all the characters in 'm_pFilter' to be processed.
> nChar = tolower(nChar);
> if ((nChar >= 'a' && nChar <= 'z') ||
> nChar == BACK_SPACE)
> {
> CEdit::OnChar(nChar, nRepCnt, nFlags);
****
This ignores your change to nChar and simply reuses the original nChar value, which it has
hidden away!
****
> }
> else
> {
> MessageBeep(MB_OK);
> }
>}
>
>
>I appreciate any help and thank you in advance.
>
>Geo
Joseph M. Newcomer [MVP]
email: EMail@HideDomain.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm