Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6753979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:13:33+00:00 2026-05-26T13:13:33+00:00

I have made the following class to avoid the repetition if else code ,

  • 0

I have made the following class to avoid the repetition if else code , again and again :

public class Error 
{

    private ErrorProvider errProvider;


    public void SetError(Control control, string value)
    {

        errProvider = new ErrorProvider();

        if (control.Text.Trim().Length == 0)
        {
            errProvider.SetError(control, value);
        }
        else
        {
            errProvider.SetError(control, "");

        }

    }


}

and validation on textbox code is

private void textBox1_Validating(object sender, CancelEventArgs e)
    {

            erp.SetError(textBox1, "Please Enter Name");

    }
  1. After typing the text in texbox, as I press enter, it working fine;
  2. Without Entering any thing in textbox, as I press Enter , it also
    working fine means Waring red icon appears; BUT
  3. after warning, as I press tab after entering text in textbox ,
    warning Icon does not disappear , it sould be removed.

without class it is working fine with the same code

update?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:13:34+00:00Added an answer on May 26, 2026 at 1:13 pm

    Use Field Initializer

    private readonly ErrorProvider errProvider = new ErrorProvider();
    

    You should use Field Initializer for errProvider, because every time calling Error.SetError, ErrorProvider will be constructed, and a constructed ErrorProvider will not only no disappear but also be just layered.


    [updated]

    public class Error
    {
        private readonly ErrorProvider errProvider = new ErrorProvider();
        public void SetError(Control control, string value)
        {
    
            if (control.Text.Trim().Length == 0)
            {
                errProvider.SetError(control, value);
            }
            else
            {
                errProvider.SetError(control, "");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made the following code:- class A{ bool bFlag[2]; public: A(){ for(int i
Suppose I have the following class: public class Foo { private List<Integer> list =
Have a look at following code: @Local public interface MyService { void printMessage(); }
I have a simple nav made up of the following HTML; <div class=inner> <div
I have roughly the following code. Could this be made nicer or more efficient?
I have the following class hierarchy in C++: class Base { virtual void apply()
I have a class that contains the following two properties: public int Id {
I have the following enum in a Java class: public enum Resolution { RES_32
I have the following ValidationRule class in WPF public class EmptyFieldValidationRule: BaseValidationRule { public
i have the following: a game class class Game { public event EventHandler GameOver;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.