Solved

Force the validation of a TextBox

Posted on 2004-04-16
5
797 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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