TextBox TextChanged event on programmatic versus user change of text contents

后端 未结 8 1714
日久生厌
日久生厌 2020-12-15 18:16

I would like to differentiate between changing the text programmatically (for example in a button click handler event) and user input (typing, cutting and pasting text). <

8条回答
  •  既然无缘
    2020-12-15 18:41

    Similar to JHunz's answer, just add a boolean member variable to your control:

    bool programmaticChange = false;
    

    When you are making programmatic changes do this:

    programmaticChange = true;
    // insert changes to the control text here
    programmaticChange = false;
    

    In your event handlers, you just need to inspect the value of programmaticChange to determine if its a programmatic change or not.

    Fairly obvious and not very elegant but its also workable and simple.

提交回复
热议问题