TextBox TextChanged event on programmatic versus user change of text contents

后端 未结 8 1727
日久生厌
日久生厌 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:51

    Depending on your exact demands you can use TextBox.IsFocused in the TextChanged event to determine manual input. This will obviously not cover all ways of programmatical changes, but works for a lot of examples just fine and is a pretty clean and save way of doing so.

    Basically this works if:
    ...the programmatical changes are all based on a manual change (e.g. a Button press).
    It will not work if:
    ...the programmatical changes are completely based on code (e.g. a Timer).

    Code example:

    textBox.TextChanged += (sender, args) =>
        if (textBox.IsFocused)
        {
            //do something for manual input
        }
        else
        {
            //do something for programmatical input
        }
    }
    

提交回复
热议问题