Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
I have a win forms 2.0 c# app that commits/processes text in a text box when it is entered by using the validated mechanism and handling the enter key. This works great when moving between controls as the focus changes which drives these events. However, if I have new data in an edit box without pressing 'enter' and I select a menu item like 'File/Save' no validation occurs when leaving the Textbox control as the focus is still on it so my data is not updated. The same situation occurs when I have a non-modal dialog and I switch in between the main app and the dialog. There is no focus change so the validation doesn't occur. The 'Leaving' event doesn't fire either.

How should this situation be handled? I see other apps that do this correctly. Is the solution the same for both the menu selection and form change and toolbar buttons?

Thanks
Posted

Have you tried the Validating event rather then the Validated event?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Mar-11 15:11pm    
This is good point, a 5. See some advanced options in my answer.
--SA
Eduard Keilholz 24-Mar-11 4:07am    
Thank you SA ;)
FlipnEh 1-Apr-11 17:34pm    
In this situation, neither the validating nor the validated events fire. Using ++spy on an equivalent mfc test app, I could not find any messages sent related to the edit box after leaving other than mouse events (there are a lot of messages spewing out so I could have missed something).

Thanks,

J
Alternative or additional to validation you can handle event System.Windows.Forms.TextBox.TextChanged. You cannot cancel the change using this event. You can only read the text on this event and act accordingly if it is invalid. Design such behavior very carefully.

To be able to cancel a text change, you can handle the event KeyPress. If you use Handled = true (a property of this event's event arguments), you can cancel the change, so in this way you can filter out unwanted characters from input.

—SA
 
Share this answer
 
v2
Comments
Eduard Keilholz 24-Mar-11 3:11am    
Good call, my 5!
Sergey Alexandrovich Kryukov 24-Mar-11 4:01am    
Thank you, Eduard.
--SA
FlipnEh 1-Apr-11 17:53pm    
The data is not usable until I know the user is done editing. I could perhaps maintain the data in some type of hold/buffer that I could pump generically on every ui event- all menu clicks, button bar clicks and possibly Form activation but this really breaks the cohesion focus of my object oriented programming. I am working with graphical data so it I type in 90 I can't have the object move to 9 and then to 90 as the user types. Thanks guys for the input. Hopefully some good information about this type of situation will show up!

Flip
Sergey Alexandrovich Kryukov 1-Apr-11 18:04pm    
How do you know? There are many cases the keystrokes should be filtered about. And they are very usual. If you require integer > 0, what's the point to type anything but digits. So...
--SA
You can try using the TextChanged event or KeyUp event. This can be painfully slow if it is a large text box but a small simple one taking only a few characters should work fine.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Mar-11 15:13pm    
It's actually very fast regardless of the length. I say so because I know how it works internally. It can be slow if the developer re-read the content of the control each time on this event for validation, for example.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900