Subscribe child controls to events

Hello,

I am in the process of writing a database-connected application.  I am using a base "data entry" form from which all data forms derive.  On the derived forms, I add a series of controls which get bound to database objects.  

On each form, I have subscribed all the child controls to a "value changed" event of some sort (text changed for textboxes, checkedChanged for checkboxes, etc.) which in turn update a boolean indicating whether the user has changed data on the page since its inception  (checking the underlying dataSet for changes will sometimes return true even if the data didn't actually "change", such as adding a blank row to emulate a "new" entry).   The "value change" events are the same for all textboxes, checkboxes, etc.

What I would like to do is create a method in my base form which automatically subscribes child controls to their specific value change event.  I don't like the method of doing this on each derived form, since its allows too much variation between forms and does not create a consistent UI (nevermind the excessive extra work it requires).

Is there a way to subscribe child to controls to an event, and possibly also subscribe "children of children" to events as well?  Iff so, how would I go about subscribing child controls to their specified events in the base form rather than each derived form?

Thanks!
TLevin10Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pgloorCommented:
This is my solution for the problem. It does not address the "children of children" problem. However, since each control has a Controls property, you might put the code into a method and call it recursively for each control.

Peter
----------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1 {
  public partial class Form1 : Form {

    public Form1() {
      InitializeComponent();
      foreach (Control control in this.Controls) {
        switch (control.GetType().ToString()) {
          case "System.Windows.Forms.TextBox":
            TextBox tb = (TextBox)control;
            tb.TextChanged += new EventHandler(textBoxControl_TextChanged);
            break;
          case "System.Windows.Forms.CheckBox":
            CheckBox cb = (CheckBox)control;
            cb.CheckedChanged += new EventHandler(checkBoxControl_CheckedChanged);
            break;
          default:
            // In this example we catch TextChanged events
            // for any other control in a common event handler
            control.TextChanged += new EventHandler(control_TextChanged);
            break;
        }        
      }    
    }

    /*
     * Common event handler for all textbox TextChanged events
     */
    private void textBoxControl_TextChanged(object sender, EventArgs e) {
      MessageBox.Show("Textbox changed");
    }

    /*
     * Common event handler for all checkbox CheckedChanged events
     */
    private void checkBoxControl_CheckedChanged(object sender, EventArgs e) {
      MessageBox.Show("Checkbox changed");
    }

    /*
     * Common event handler for all other controls TextChanged events
     */
    private void control_TextChanged(object sender, System.EventArgs e) {
      if (sender.GetType().ToString().EndsWith("Button")) {
        // Place your code specific to buttons here
      } else if (sender.GetType().ToString().EndsWith("Label")) {
        // Place your code specific to labels here
      }
    }
  }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.