Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# UI Forms NET
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 23-Mar-11 9:56am
FlipnEh266
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Have you tried the Validating event rather then the Validated event?
  Permalink  
Comments
SAKryukov at 23-Mar-11 15:11pm
   
This is good point, a 5. See some advanced options in my answer.
--SA
Eduard Keilholz at 24-Mar-11 4:07am
   
Thank you SA ;)
FlipnEh at 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
Rate this: bad
good
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.
 
—SA
  Permalink  
v2
Comments
Eduard Keilholz at 24-Mar-11 3:11am
   
Good call, my 5!
SAKryukov at 24-Mar-11 4:01am
   
Thank you, Eduard.
--SA
FlipnEh at 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
SAKryukov at 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
Rate this: bad
good
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.
  Permalink  
Comments
SAKryukov at 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)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 BillWoodruff 220
4 Mathew Soji 195
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,817
2 DamithSL 5,461
3 Manas Bhardwaj 4,946
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 24 Mar 2011
Copyright © CodeProject, 1999-2014
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