11

How can I detect changes in any control of the form in C#?

As I have many controls on one form and i need to disable a button if any of the control value in the form changes. I am in search of some built-in function/event-handler/property, and don't want to make a customized function for that.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Asad
  • 521
  • 2
  • 13
  • 30
  • Gratulations for the mislabeling - the same way you do in VB.NET or while eating pizza. What Controls do you talk about? WPF or WinForms? – TomTom Aug 26 '10 at 02:51
  • I mean, i need to detect changes i the text boxes/combobox/checked box values,if there is any change detected from the previously filled form values , disable a button only. – Asad Aug 26 '10 at 02:54
  • The application is a windows form application built in using VS c#.net – Asad Aug 26 '10 at 02:56

3 Answers3

17

No, I'm not aware of any event that fires whenever any control on the form changes.

My advice would be to subscribe to each event individually (if your form has so many controls that this is actually difficult to do, then you may want to re-think your UI).

If you absolutely must subscribe to changes to all controls then you might want to consider something similar to the following:

foreach (Control c in this.Controls)
{
    c.TextChanged += new EventHandler(c_ControlChanged);
}

void c_ControlChanged(object sender, EventArgs e)
{

}

Note that this wouldn't work particularly well however if you dynamically add and remove controls to the form at runtime.

Also, the TextChanged event might not be a suitable event for some control types (e.g. TextBoxes) - in this case you will need to cast and test the control type in order to be able to subscribe to the correct event, e.g.:

foreach (Control c in this.Controls)
{
    if (c is CheckBox)
    {
        ((CheckBox)c).CheckedChanged += c_ControlChanged;
    }
    else
    {
        c.TextChanged += new EventHandler(c_ControlChanged);
    }
}
Justin
  • 84,773
  • 49
  • 224
  • 367
  • 5
    you also run into the problem that this method doesnt deal with sub controls, such as textboxes in a panel/flowLayoutPanel/tableLayoutPanel etc. – Pondidum Aug 26 '10 at 06:55
  • If you want to get all controls in your form (include child controls) to apply this solution then read [this answer](https://stackoverflow.com/a/42675541/4927427). – tungns Feb 01 '21 at 09:00
5

Instead of accessing the controls directly you can databind to a model object that implements INotifyPropertyChanged.

Whenever the user does something that causes the data in your model to change you will be notified and can take the appropriate action.

It will probably also cut down on the amount of code you need to get values in and out of the form controls.

Andrew Kennan
  • 13,947
  • 3
  • 24
  • 33
  • This is not want the OP had asked for. Having to create a separate class that implements INotifyPropertyChanged would make it too complicated for this simple requirement – Peter M. Feb 04 '21 at 13:53
0

Simply select the components that will have common handler and in the events property bar double click to event that you want to occur. This common method will be added automatically to all control's handlers.

İlkin
  • 31
  • 5