How to detect changes in any control of the windows form in C#?

Updated on     Kisan Patel

Problem:

How to detect changes in any control of the windows form in C#?
detect changes in windows form c#
track changes in windows form C#
How to detect if user has made changes to a windows application in C#

Solution:

There is no event that fires whenever any control on the form changes.

You 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)
{
    //Login for control change
}

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

For example:

foreach (Control c in this.Controls)
{
    if (c is CheckBox)
    {
        ((CheckBox)c).CheckedChanged += c_ControlChanged;
    }
    else
    {
        c.TextChanged += new EventHandler(c_ControlChanged);
    }
}

Source: http://stackoverflow.com/questions/3571722/how-to-detect-changes-in-any-control-of-the-form-in-c


C#

Leave a Reply