[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1021
  • Last Modified:

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?

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


mikexpertAuthor Commented:
Thanks again Mahesh!  

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now