1

The .TextChanged event on a TextBox fires only after the TextBox loses focus. Is there a way to detect every character that is being typed while the user is typing it?

I know the radio button has a similar event called CurrentCellDirtyStateChanged to detect when the button is clicked without having to wait for it to lose focus. I was trying to find something similar for a TextBox.

JDB
  • 25,172
  • 5
  • 72
  • 123
John
  • 1,310
  • 3
  • 32
  • 58
  • possible duplicate of [Only allow specific characters in textbox](http://stackoverflow.com/questions/12607087/only-allow-specific-characters-in-textbox) – JDB Feb 28 '14 at 17:32

2 Answers2

5

It is not true that the TextChanged event is triggered only when the textbox loses the focus. This code

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) _
    Handles TextBox1.TextChanged

    System.Diagnostics.Debug.WriteLine("TextBox1.Text = " & TextBox1.Text)
End Sub

Yields the following output (in the Output window) when typing "Hello":

TextBox1.Text = H
TextBox1.Text = He
TextBox1.Text = Hel
TextBox1.Text = Hell
TextBox1.Text = Hello

However, it will also fire when you delete characters, so if your intention is to filter characters use one of the key events.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • You are correct. However, it wasn't working for me when I was testing it. I still don't know why, but it is working now. Thanks. – John Mar 05 '14 at 14:24
3

You could use the KeyDown or KeyPress events. These will fire whenever you press a key.

JDB
  • 25,172
  • 5
  • 72
  • 123