.NET: TextChanged event for TextBox does not always fire, even if KeyUp and KeyDown fire

I ran into a very peculiar problem. I noticed that sometimes during text input in a TextBox I lose a few keystrokes. I added a bunch of trace statements to the events triggered by this TextBox, and I found that when I lost the keys, the KeyUp, KeyDown, and KeyPress events were fired correctly, but the TextChanged event never fired.

Does anyone have any idea why this will happen? I could write this as a “.NET error,” but I would rather find out if there is a solution here.

If there is an assumption that I am using KeyUp / KeyDown events to determine if the text has been changed, there is a problem there as well. KeyUp / KeyDown is called several times for each key press, so it would be very difficult to determine if someone typed the same letter several times.

+3
source share
3 answers

Mmm ...

It will be a shot, but you said that you have KeyUp, KeyDown, and KeyPress event handlers? Have you set the e.Handled flag to true in event handlers, look here:

        private void textBox1_KeyDown (object sender, KeyEventArgs e)
        {
            e.Handled = true;
        }

        private void textBox1_KeyUp (object sender, KeyEventArgs e)
        {
            e.Handled = true;
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

, MSDN . ( MSDN 2008 SP 1 , ms-help://MS.MSDNQTR.v90.en/fxref_system.windows.forms/html/dfc80b44-1d79-6315-cbea-1388a048c018.htm)

:

Handled is implemented differently by different controls within Windows Forms. 
For controls like TextBox which subclass native Win32 controls, it is 
interpreted to mean that the key message should not be passed to the underlying 
native control. 

If you set Handled to true on a TextBox, that control will not pass the key 
press events to the underlying Win32 text box control, but it will still 
display the characters that the user typed. 

, i.e. e.Handled = false;, TextChanged?

?

: dreadprivateryan, (- ), , e.Handled , Enter false , , , .

  • Enter? , KeyUp, KeyDown ...
  • ...
  • , , , KeyDown, KeyUp Event Handler, , , , , , , , . . , SO.

keyUp . KeyPress 0-9, . - backspace .

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) SendKeys.Send("{TAB}");
        }
        private const string VALID_KEYS = "0123456789";
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
        {                  
            if (VALID_KEYS.IndexOf(char.ToUpper(e.KeyChar)) != -1 || e.KeyChar == (char)8) 
                 e.Handled = false;                                                               
            else                                                                                 
                 e.Handled = true;                                                                
}                                                                                        

, , , .

+2

, : ?

, , - , . , . , , .

+1

, ? , TextChanged ?

, TextChanged EN_CHANGE, WM_COMMAND. , Windows "", . , WM_MOUSEMOVE , .

, , TextChanged . , . TextChanged .

0

Source: https://habr.com/ru/post/1727438/


All Articles