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

Posted on 2006-04-11
Last Modified: 2013-11-20
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?

Question by:mikexpert
    LVL 22

    Accepted Solution

    >>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);



    Author Comment

    Thanks again Mahesh!  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Title # Comments Views Activity
    xyBalance chalenge 58 77
    Prevent this page from creating additional dialogs. 3 155
    viewing source code from eclipse 13 64
    EvenOdd challenge 10 67
    Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now