• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

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!
0
TLevin10
Asked:
TLevin10
1 Solution
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now