Itโs enough for you to watch. You may need to add additional loops foreachfor other types controlas needed. It's nice that you only need a few lines of code for the type control, and not for each instance, with this approach.
private void addHandlers()
{
foreach (TextBox control in Controls.OfType<TextBox>())
{
control.TextChanged += new EventHandler(OnContentChanged);
}
foreach (ComboBox control in Controls.OfType<ComboBox>())
{
control.SelectedIndexChanged += new EventHandler(OnContentChanged);
}
foreach (CheckBox control in Controls.OfType<CheckBox>())
{
control.CheckedChanged += new EventHandler(OnContentChanged);
}
}
protected void OnContentChanged(object sender, EventArgs e)
{
if (ContentChanged != null)
ContentChanged(this, new EventArgs());
}
public event EventHandler ContentChanged;
After changing the method addHandlersto support all your controls and invoking it after adding all the controls to your form, you can simply subscribe to the event ContentChangedto perform any actions that may occur at any time the form has changed (i.e. enable / disable the save button) .
Servy source
share