Validating Integer Problems....

T

TheSteph

Hi Experts !



I have a Winform Program in C# / .NET 2.0



I would like to ensure that a value in a TextBox is a valid Int32 when user
get out of it (TextBox loose focus)



BUT : The "Validating" Event of a TextBox does NOT fire when user click on
controls like ToolStripButton, ToolStripMenu,.. (in fact Validating event is
fired AFTER the Click event of the ToolStripButton, so it's too late to
check...)



In this kind of case, does it mean that a programmer using .NET MUST check
at more than one place that the value in a given TextBox is a valid Integer
? (Once in the validating event, and Once in EACH ToolStripButton(menu,..)
Click) ???



If no : HOW CAN I DO to check if a TextBox have a valid Int32 value when
TextBox loose focus AND when user click on a ToolStripButton (or menu)
?????????



If yes : How can MS think that it's a good programming practice and modern
to check the same basic and simple thing at different places in the same
form. Why this kind of problem in the "Third version of their very last
programming language" ?



Any solution is welcome and will save me a lot of headache..



Steph
 
G

Guest

I would go in a different direction, I will not let the user enter
non-numeric values

There are several ways of doing this and IIRC in 2.0 you have something to
force this.

Anyway, you can use the KeyDown event to reject the invalid keys.

additionally you can derive from Textbox and override CreateParams
 
T

TheSteph

Anyway, you can use the KeyDown event to reject the invalid keys.

All sample code I found accept numbers like 9999999999 that are build from
correct keys but are not valid Int32 values...

Moreover I have some "encoding helper" job to do while validating the
final value : Example : 12,3456,789.25 should be accepted and transformed to
123.456.789,25 ( "," is decimal sep here). This kind of things can be done
only at Validating time. hence the problem remains with controls like
ToolStripButtons.

The things I can't understand is that Controls have a Validating event
that is not guaranteed to be fired when user click on other controls (like
ToolStripButton,.)...unbelivable... Imagine a click event on a control that
is not guaranteed to be fired when user click the control. unreliable so
unusable from a professional standpoint..

Thanks for your help, I keep searching a validating solution. The best is
maybe to wait until VS "Hawaii" to be done...



Steph.
 
G

Guest

Hi,

What make u think that it will be different in "hawaii" ?
you can do validation in the KeyDown, the ugliest one would be trying to
convert it to Int32 and handling the exception.

Take a look at CausesValidation though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top