Click here to Skip to main content
12,399,975 members (48,959 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# .NET UI Forms
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?

Posted 23-Mar-11 8:56am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Have you tried the Validating event rather then the Validated event?
SAKryukov 23-Mar-11 15:11pm
This is good point, a 5. See some advanced options in my answer.
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).


Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.

Eduard Keilholz 24-Mar-11 3:11am
Good call, my 5!
SAKryukov 24-Mar-11 4:01am
Thank you, Eduard.
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!

SAKryukov 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...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
SAKryukov 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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 24 Mar 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100