Subscribe child controls to events

Posted on 2006-04-20
Last Modified: 2012-06-21

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?

Question by:TLevin10
    1 Comment
    LVL 5

    Accepted Solution

    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.

    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() {
          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);
              case "System.Windows.Forms.CheckBox":
                CheckBox cb = (CheckBox)control;
                cb.CheckedChanged += new EventHandler(checkBoxControl_CheckedChanged);
                // In this example we catch TextChanged events
                // for any other control in a common event handler
                control.TextChanged += new EventHandler(control_TextChanged);

         * 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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now