Solved

Force the validation of a TextBox

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now