tags:

views:

324

answers:

3

Is there any way to validate the contents of a CEdit box without subclassing?

I want to check for invalid filename characters in a CEdit box and not allow the user to input it at all (keypress should not be recorded, if pasted in the box, the invalid characters should just not make it to the edit box)..

Is there any easy way to do this?

On a side note, how do I make a variable that is tied to this box? If I add a variable that is not a control one, would this variable always contain what is in the edit control?

Thanks..

A: 

Per http://msdn.microsoft.com/en-us/library/f7yhsd2b(VS.80).aspx , "If you want to handle Windows notification messages sent by an edit control to its parent (usually a class derived from CDialog), add a message-map entry and message-handler member function to the parent class for each message." and "ON_EN_UPDATE The edit control is about to display altered text. Sent after the control has formatted the text but before it screens the text so that the window size can be altered, if necessary." -- so, without subclassing CEdit, you can vet and possibly block updates via the windows message-map/message-handler in the parent.

Alex Martelli
So how do I alter the text? I tried doing GetWindowText(temp), then doing stuff like temp.Replace("*",""); // temp is a CString, then using SetWindowTExt(temp). This does not work as I get stack overflow.. I think it might be because when i do SetWindowText it calls the notification over and over again...
krebstar
Yes, you're in a recursion, so you simply need to break it (by NOT doing the SetWindowText) once, for example, there are no stars left in the text you get (supposing that removal of stars is your goal).
Alex Martelli
+1  A: 
Anton Gogolev
I wanted an easy way :(
krebstar
A: 

Override PreTranslateMessage in your dialog. When the mesage is WM_KEYDOWN and GetFocus() returns the HWND of your edit control, eat the message when you don't like the key.

Aidan Ryan