Solved

Force the validation of a TextBox

Posted on 2004-04-16
5
792 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

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…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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