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

  • SEARCH
  • Home
  • 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 9226253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:47:06+00:00 2026-06-18T04:47:06+00:00

An apply button on my standard winform is disabled on loading and I want

  • 0

An apply button on my standard winform is disabled on loading and I want to enable it, if a state of any other control changes (checkboxes, radioboxes, textboxes etc.)

So I could go through the events of every single control and enable the button there but I want to know if there is an easier way to do it, like a global event.

Edit
To clarify: I’m looking for a method where I havn’t to do something with every single control. So that I can add more controls at a later point and don’t have to care about them …

  • 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-06-18T04:47:08+00:00Added an answer on June 18, 2026 at 4:47 am

    As per your edit I’ve revised my answer to include the functionality you desire.

    First you need a generic event handler that should look something like this:

    void MyHandler(object obj, EventArgs e)
    {
        button1.Enabled = true;
    }
    

    Where button1 is the button you wish to enable.

    Then you’re going to need a recursive method to not only iterate through your Form.Controls, but any container type controls contained in there as well. I’ve included handling of some common container controls as well as some basic data entry controls:

    void AddEvents(System.Windows.Forms.Control.ControlCollection Controls)
    {
        foreach (Control c in Controls)
        {
            if (c is GroupBox)
            {
                AddEvents(((GroupBox)c).Controls);
            }
            else if (c is Panel)
            {
                AddEvents(((Panel)c).Controls);
            }
            //Expand this series of if...else... to include any 
            //other type of container control
            else if (c is TextBox)
            {
                ((TextBox)c).TextChanged += new EventHandler(MyHandler);
            }
            else if (c is RichTextBox)
            {
                ((RichTextBox)c).TextChanged += new EventHandler(MyHandler);
            }
            else if (c is CheckBox)
            {
                ((CheckBox)c).CheckedChanged += new EventHandler(MyHandler);
            }
            else if (c is DateTimePicker)
            {
                ((DateTimePicker)c).ValueChanged += new EventHandler(MyHandler);
            }
            //Expand this to include any other type of controls your form 
            //has that you need to add the event to
        }
    }
    

    The first part of the if else block checks to see if the control is of a type that contains other controls. If it is it then recursively calls the AddEvents method with the new System.Windows.Forms.Control.ControlCollection contained in that control.

    The second part of the if else block checks what type of control c is so that it can be appropriately cast to the correct type, and therefore utilize the correct event. If we are able to determine our control type at this point, the generic event created earlier is added as a handler.

    Last, you need to call this method. The two best places would probably be either in your constructor or on the Form.Load event. The best place to put it will depend on your specific circumstances. I chose to use my constructor for simplicity, which now looks like this:

    public Form1()
    {
        InitializeComponent();
        AddEvents(this.Controls);
    }
    

    And that should be all you need to iterate your controls and add the generic event handler. This code is from an actual project I created and I have tested it to ensure it’s proper functionality.

    EDIT: I’ve also just tested this using controls inside of a GroupBox inside of a Panel inside of a GroupBox inside of a Panel that’s on a Form. This is where the usefulness of utilizing recursion comes in. You don’t need to know to exact depth of nesting so long as you properly set up your if...else... blocks. It will go as deep as it needs to without needing to use nested loops and knowing the exact depth.

    EDIT2: As a side note, this method could also be used on a more granular level. Let’s say you have multiple GroupBox controls and you wish to only add the event handler to controls in “grpBox1”. You could call AddEvents(grpBox1.Controls) instead of AddEvents(this.Controls) and this would only apply the event handler to controls contained in grpBox1.

    EDIT3: As onemancat has pointed out in the comments, it’s not entirely necessary to actually check if the control is a GroupBox or a Panel etc. because all controls inherit from the base class Control which has a Controls property. You simply could check if the Control contains other controls by saying if (c.Controls.Count > 0) AddEvents(c.Controls); however, in a situation where one wants to choose which container controls to iterate, it would be necessary to check the type as I have in my example. If it’s not necessary to be this granular it does make more sense to check the count and never bother with type checking or casting.

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

Sidebar

Related Questions

I want to apply scaling animation to UIButton.When user drag button upside of the
How to set a bitmap as a button so that i can apply button
i get confuse in apply and save button. whether Save will save the settings.
I'm trying to apply a DataTrigger to a Button and it depends on a
I have a several buttons in my application and I want to apply a
I'd like to change the color of a standard Android button slightly in order
I've developed a custom control for a button that can have an image and
When the user clicks the OK or APPLY button on a property sheet and
I'd like to change the text on a property sheet APPLY button but I
I'm trying to apply css with GWT Button widget, however I can apply CSS

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.