Detecting Form Field Changes in Windows Forms using C#

Sometimes we may need to have a windows form that has lot of textboxes, dropdown lists, radio buttons, checkboxes etc. If is difficult to determine whether the form fields are changed by checking each and every fields with their respective initial values.

In this case, you can try to implement a MessageFilter to the windows form that detect key down and left mouse up events and check if current focused control is changed if user generate these two events. It means that the value of the current focused control assumed to be changed if user pressed a key or clicked left mouse on it.

IMessageFilter in System.Windows.Forms namespace is an interface that allows an application to capture a message before it is dispatched to a control or form.

A class that implements the IMessageFilter interface can be added to the application’s message pump to filter out a message or perform other operations before the message is dispatched to a form or control.

The class associated with a Windows Form control should be inherited from both System.Windows.Forms.Form and System.Windows.Forms.IMessageFilter in order to implement this functionality.

Here is the code.

using System;
using System.Windows.Forms;

namespace WinFormTest1
    public partial class MessageFilterDemoForm : Form, IMessageFilter
        const int WM_KEYUP = 0x101;
        bool isFormFieldChanged = false;

        public MessageFilterDemoForm()

        public bool PreFilterMessage(ref Message m)
            bool ret = true;
            if (m.Msg == WM_KEYUP)
                if (this.ActiveControl is TextBox)
                    isFormFieldChanged = true;
                    ret = true;
                ret = false;
            return ret;

        private void button1_Click(object sender, EventArgs e)
            MessageBox.Show("isFormFieldChanged : " + isFormFieldChanged.ToString());


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: