CEdit::OnChar not working as I expect...

Hi Experts,

I need to be informed of how to use OnChar to change the character being typed.  Basically, what I'm trying to do is modify the contents of my CEdit control - so that when I enter a postal code, I ensure that the user is entering the proper type of character at least.

Example  J3H 4H8   (i.e., the format of Canadian postal codes are [letter, number, letter, space, number, letter, number]).

So if I'm in the 3rd position, after the H was just typed, I want to insert a space automatically.

but my class that overrides CEdit doesn't work as I expected.  When I call CEdit::OnChar(-1,-1,-1), I still get the character that was typed in...  (even CEdit::OnChar(-1,-1,-1)
void CValidatingEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
      CEdit::OnChar(' ', 1, nFlags);
}

Of course this is not the code needed to do what I entered above - I just wanted a proof of concept that adding a space was possible and easy to do... To me, the above function should insert a space for any character that was pressed - but that's not what happens... How come?

Thanks!
Mike
mikexpertAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mahesh1402IT ProfessionalCommented:
>>To me, the above function should insert a space for any character that was pressed - but that's not what happens... How come?

Did you see the note at the bottom of the OnChar documentation?...

"...This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function".


...try calling DefWindowProc with a reconstituted WM_CHAR message, and only call CEdit::OnChar when you want the thing to pass through as-is.


Here's a real quick example...


    VOID  CSomeEdit::OnChar(  UINT nChar,  UINT nRepCnt, UINT nFlags  )
    {
                    DefWindowProc( WM_CHAR, (WPARAM)_T(' '), GetCurrentMessage()->lParam);
     }


MAHESH

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikexpertAuthor Commented:
Thanks again Mahesh!  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.