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 7062217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:34:02+00:00 2026-05-28T04:34:02+00:00

I am making an extension method library to use in windows form applications. One

  • 0

I am making an extension method library to use in windows form applications. One of the methods I intend to create will make setting error states on input controls easier, e.g.

public static void SetError(this System.Windows.Forms.TextBox textBox, string errorMessage)
{
    if (string.IsNullOrEmpty(errorMessage))
    {
        //reset control state
        textBox.BackColor = System.Drawing.SystemColors.WindowText;
    }
    else
    {
        //set background colour to a nice shade of red
        textBox.BackColor = System.Drawing.Color.MistyRose;
    }

    //try to locate an ErrorProvider on the control's containing form.
    var errorProvider = LocateErrorProvider(textBox);

    if (errorProvider != null)
    {
        //set error message on error provider (or clear it)
        errorProvider.SetError(textBox, errorMessage);
    }
}

I am trying to figure out the LocateErrorProvider method. What I would like to do is check whether an ErrorProvider exists on my form and then only make use of it if it does exist.

ErrorProvider is a Component and not a Control, so I can’t get to it via the form.Controls property. I have tried casting the parent form into a variety of objects, to no avail.

UPDATE: I have managed to get to the ErrorProvider using reflection using the following code:

private static System.Windows.Forms.ErrorProvider GetErrorProvider(System.Windows.Forms.Control control)
{
    //get the containing form of the control
    var form = control.GetContainerControl();

    //use reflection to get to "components" field
    var componentField = form.GetType().GetField("components", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

    if (componentField != null)
    {
        //get the component collection from field
        var components = componentField.GetValue(form);

        //locate the ErrorProvider within the collection
        return (components as System.ComponentModel.IContainer).Components.OfType<System.Windows.Forms.ErrorProvider>().FirstOrDefault();
    }
    else
    {
        return null;
    }
}

Personally, I’m not too fond of using the hard-coded field name to get to the field. But in this case it seems to work just fine. Does anybody have a better way to achieve the same results?

  • 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-28T04:34:03+00:00Added an answer on May 28, 2026 at 4:34 am

    So far, this seems to solve my problem:

    private static System.Windows.Forms.ErrorProvider GetErrorProvider(System.Windows.Forms.Control control)
    {
        //get the containing form of the control
        var form = control.GetContainerControl();
    
        //use reflection to get to "components" field
        var componentField = form.GetType().GetField("components", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    
        if (componentField != null)
        {
            //get the component collection from field
            var components = componentField.GetValue(form);
    
            //locate the ErrorProvider within the collection
            return (components as System.ComponentModel.IContainer).Components.OfType<System.Windows.Forms.ErrorProvider>().FirstOrDefault();
        }
        else
        {
            return null;
        }
    }
    

    Thanks to Hans & Cody for their excellent ideas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

C# 3.0 Extension methods add extensions to the base Type making calling that method
Im making firefox extension. One function stores value in every tab, to use it
I've added some extension methods for strings to make working with some custom enums
I am making the following call to an extension method: database.ExecuteScalar(command).NoNull<string>(string.Empty); I get an
I have found several links to methods of making extension methods work in .NET2.0
I'm making a Chrome extension and am wondering, what is the best method for
I am making a Firefox extension and would like to give my users more
I am making a Google Chrome extension and I would like to have my
I am making extensive use of boost:shared_ptr in my code. In fact, most of
Making an adobe flex ui in which data that is calculated must use proprietary

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.