locked
User event VS property change event ? RRS feed

  • Question

  • Hi all!

    When using a CheckBox, I have not found a way to differentiate between a user generated event and the application directly changing the Checked property. In both case the CheckedChanged event is triggered.

    The same goes for other WinForm controls such as TextBox or NumericUpDown.

    So my question is:
     - What is the general approach in WinForm to differentiate between an event triggered by an user action (click, hit space bar, hit return, cut&paste, etc...) and an event triggered by the application changing a property?

    Thanks,

    Antoine
    Tuesday, March 13, 2007 4:09 PM

Answers

  • This is (I believe) the standard pattern:

      public partial class Form1 : Form {
        private bool mBusy;
        public Form1() {
          InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e) {
          mBusy = true;
          try {
            checkBox1.Checked = !checkBox1.Checked;
          }
          finally {
            mBusy = false;
          }
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e) {
          if (mBusy) Console.WriteLine("Changed by code");
          else Console.WriteLine("Changed by user");
        }
      }

    Saturday, March 24, 2007 7:00 PM