473,320 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Right way to perform validation on a text box

Hi,

Here is the desire behavior, when the user tries to leave a field, that
field is validated. If the value in the field is invalid, the previous
value is restored - however, the user is allowed to go on to the next field
anyway.

It seems that the Validating event has the behavior of keeping the user in
the field if e.Cancel is set to True - and doesn't replace the bad value
with the last good one. Which is all very undesirable! It means the user
can't even close the program until a valid value is entered into the field!
Why would anyone want this behavior?

Is there any built in way to get the functionality I am looking for, or does
it all have to be done manually?

Thanks
Jul 21 '05 #1
2 1727
I do not know of a built-in way to do what you want... but you can easily
define a class that inherits from System.Windows.Forms.TextBox, and adds
that functionality. Simply define all the textboxes on your form to derive
from your class instead of directly from System.Windows.Forms.TextBox

That's not so hard.

"Marina" <so*****@nospam.com> wrote in message
news:O2*************@TK2MSFTNGP14.phx.gbl...
Hi,

Here is the desire behavior, when the user tries to leave a field, that
field is validated. If the value in the field is invalid, the previous
value is restored - however, the user is allowed to go on to the next field anyway.

It seems that the Validating event has the behavior of keeping the user in
the field if e.Cancel is set to True - and doesn't replace the bad value
with the last good one. Which is all very undesirable! It means the user
can't even close the program until a valid value is entered into the field! Why would anyone want this behavior?

Is there any built in way to get the functionality I am looking for, or does it all have to be done manually?

Thanks

Jul 21 '05 #2
On Mon, 4 Oct 2004 09:58:31 -0400, Marina wrote:
It seems that the Validating event has the behavior of keeping the user in
the field if e.Cancel is set to True - and doesn't replace the bad value
with the last good one. Which is all very undesirable! It means the user
Keep the value of the field in a class member or in a variable. Then in
the validating event, if the value in the textbox is not valid restore the
textbox from the variable. If the value is ok, then in the validated
event, set the variable with the value in the text box.

Something like this (pseudocode):

Private txtBoxValue As String

Private Sub TextBox1_Validating(...)
If Not TextBoxContentsValid Then
TextBox1.Text = txtBoxValue 'Restore previous value
e.Cancel = True
End If
End Sub

Private Sub TextBox1_Validated(...)
txtBoxValue = TextBox1.Text 'Save the good value
End Sub

Never use interface elements (textboxes, etc.) to be the storage for your
data. Always use a class or variable and only use interface elements to
reflect what is contained in the variable.
can't even close the program until a valid value is entered into the field!
Why would anyone want this behavior?


As far as not being able to close the app if the field is not valid, I
tend to use a close button that has it's CausesValidation property set to
False so that validation does not occur when I click that button.

HTH

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tim Frawley | last post by:
Source code attached indicates my problem with validation and a button bar save button. Fill the Textbox with some text then tab off the control. The message box will display the text in the...
2
by: Marina | last post by:
Hi, Here is the desire behavior, when the user tries to leave a field, that field is validated. If the value in the field is invalid, the previous value is restored - however, the user is...
5
by: Micky | last post by:
VB v7.1.3088 NET v1.1.4322 SP1 My mate has a strange problem regarding the ESC key and validation. When he hits the Cancel button on his form, the form does not validate. This is correct...
5
by: Lloyd Dupont | last post by:
On my page the user is ask to set of question. Say there I have 2 controls like that == pseudo-C#-code for my control == class MyControl : CompositeControl { TextBox text; LinkButton button ...
2
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
1
by: Niclas | last post by:
Hi, How do I indicate what field fails a validation, usually you see a red star next to the faild field on a form. Is this built in functionality in the valdaition controls or shall I code this...
0
by: dhurwitz | last post by:
Hi, In my ASP.NET 2.0 web app, I have a user control, ucBinEdges, with several text boxes. The user control has several validation controls validating the Text of these text boxes -...
6
by: Mike Chen | last post by:
We know in server code, we can check the page validated by using Page.IsValid where we put some validator controls on aspx page. I want to set some value after validating user input values on...
18
by: ChipR | last post by:
I have a text box with a validation rule and validation text. When entering a new record, if I put in invalid text, the validation text is displayed in a message box, but after clicking OK, another...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.