How to use error provider at run time along with associating any control to validate?

In most cases you really only need one instance of ErrorProvider on a form.

Up vote 2 down vote favorite share g+ share fb share tw.

I am trying to create a Validation in a reusable fashion. Purpose: Make the validation control reusable. Error Provider should associate with control passed dynamically and can be set or cleared at run time.

When user press OnClick event then all the controls gets validated with their own Error Providers. Public bool IsFieldEmpty(ref TextBox txtControl, Boolean SetErrorProvider,string msgToShowOnError) { ErrorProvider EP = new ErrorProvider(); if (txtControl. Text == string.

Empty) { if(SetErrorProvider==true) EP. SetError(txtControl, msgToShowOnError); return true; } else { if(SetErrorProvider==true) EP.Clear(); return false; } } Issue: Every time the function is called new errorprovider object gets created which I don't want. Every control should not have more than 1 error provider and I should be able to search it just like as done in asp.net to search for some control on a Page.

How can I do this c# .net winforms validation errorprovider link|improve this question edited Apr 20 '10 at 5:19 asked Apr 20 '10 at 5:14Shantanu Gupta3,15031870 81% accept rate.

– Darin Dimitrov Apr 20 '10 at 5:15 @Darin: sry I thought it was control integration – Shantanu Gupta Apr 20 '10 at 5:18 Just answered this on your previous question )-: – Henk Holterman Apr 20 '10 at 9:14.

In most cases you really only need one instance of ErrorProvider on a form. E.g. ErrorProvider errorProvider1 = new ErrorProvider(); or just drag one from the toolbox onto the form.

When calling an ErrorProvider, you supply the control and the message,, errorProvider1. SetError (dateTimePicker1, "HEY BAD DATE"); To clear the error... errorProvider1. SetError (dateTimePicker1, ""); So, one ErrorProvider instance is all you really need in most situations.

Sky: Thx for help. I am able to do with single EP only. But I was thinking to make it work for multiple contols.

If user submits form. Then all EP becomes visible at a time so that user need not press submit btn every time to pass correct value – Shantanu Gupta Apr 20 '10 at 5:25 @Shantanu - just change your code, move the ErrorProvider EP = new ErrorProvider(); outside the function and into the form so you have just one EP. EP is not control specific, that is why you pass in the control when setting an error.

Is there something that is missing from your question that would indicate that this is not a viable option? – Sky Sanders Apr 20 '10 at 5:43 I only want to create instance of EP at runtime when I want to validate. I.e.

SetErrorProvider filed passed in function will tell me whether to create error provider for the current control. Now here at this point I want to check whether this current control already contain any error provider or not. If no create it else find that error provider for that control and do your work.

I hope my question would be clear to you. Please let me know any suggestion. – Shantanu Gupta Apr 20 '10 at 5:52 @Shantanu - I can see no compelling reason to add complexity to a very simple scenario.

Controls do NOT contain ErrorProviders. An Error provider is an autonomous component that does not know, nor needs to know anything about the control that is being validated. There is typically no need to have more than one EP.

I think you may not have a clear understanding of the function of the EP. Please see msdn.microsoft.com/en-us/library/… and walk through the sample. I think things will become clear.

– Sky Sanders Apr 20 '10 at 5:59.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions