Solved

Force the validation of a TextBox

Posted on 2004-04-16
5
789 Views
Last Modified: 2008-01-09
Hello,

I have a form with several custom textboxes. For each textbox, I have overriden the OnValidating method (or handling the Validating event) to validate the input and display an error using an errorProvider.
So the validation is done when the user is filling the form.

But when I want to save the form, I want to revalidate everything (to get some Ids from database, ...). To do that I want to force the validation of each control and I was looping on all controls and calling the Focus method.
This is quite dirty I think and visually, the effect is not really nice.

So I was thinking about implementing a Validate public method on my Textbox. This method will call internally the protected OnValidating method. The result will be the same but without any ugly visual effect.

What is your opinion about that solution ? Don't you think that using a public method to call a protected method is breaking the encapsulation of the Textbox ?
Do you have any other / better solution ?
Thanks !

Jarod
0
Comment
Question by:Jarodtweiss
  • 2
  • 2
5 Comments
 
LVL 9

Assisted Solution

by:malharone
malharone earned 80 total points
ID: 10845273
this is a great article .. hope it gives you better direction for this problem

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms03162004.asp
0
 
LVL 4

Author Comment

by:Jarodtweiss
ID: 10845819
yes I know that article.
It is a different kind of validation and I didn't want to migrate to that kind of validation.
I wanted a kind of textboc autovalidating itself...
0
 
LVL 5

Expert Comment

by:Xentor_
ID: 10845891
Hmm... Other possible solutions...

1) Make a globally-scoped object that'll raise an event on command.  Have all of the textboxes automatically add a handler to this event on creation, and have them revalidate when it's triggered.

2) Create a shared Validate method in your text box class.  When a textbox is created, it could add a weak reference to itself into a private shared collection.  The Validate method could iterate through the collection and trigger the Validate for each object.

3) Have the textboxes automatically bind one of their properties to the form's tag, for example.  When that property changes, trigger an update.

Just some random thoughts... Not very good, but maybe they'll inspire you.
0
 
LVL 4

Author Comment

by:Jarodtweiss
ID: 10848335
Before accepting one answer, I'm waiting for one answer. What do you think of implementing a Validate method that will encapsulate a call to the OnValidate method ?
0
 
LVL 5

Accepted Solution

by:
Xentor_ earned 170 total points
ID: 10850195
Well, I don't know if that breaks the almighty rules of OOP, but I would probably put the code in a public function and have the OnValidate override call that function instead.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FInd Image Control Gridview 3 33
Finding the IIS version 5 37
Problem to Office 1 39
Asp.Net Session Question 2 33
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question