简体   繁体   中英

How to identify whether the text changed in textbox either by programmatically or by user

We are using a TextBox to display input received from the I/O system. If the user enters some data on the text box, the value will be written to the IO system.

We are using the OnTextChanged event to write the data entered by the user to the IO system.

The problem is that we get this event when we update the value received from the IO system to the text box (from the code).

Is it possible to know whether the value of the TextBox is changed by the user or by using the code?

You could set a Boolean variable named IsUserInput , after the I/O system sends data to the text box, you need to set IsUserInput to false. In the KeyDown event of the TextBox, the variable can be set to true. Finally, you could use this variable to determine the text inputter in the textChanged event.

Please refer to the following code.

public Boolean IsUserInput;

private void Button_Click(object sender, RoutedEventArgs e)
        {
            IsUserInput = false;
            myTextBox.Text = "hellohello";
          
        }

        private void myTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (IsUserInput)
            {
//write data to I/O sytem
            }
           
        }

private void myTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            IsUserInput = true;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM