Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1- When i enter "1" in
TextBox1_KeyPress
event value not be showing in TextBox1

2- When i enter "1" in
TextBox1_TextChanged
event value showing in TextBox

What I have tried:

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
      Array(0) = TextBox1.Text ' Showing
  End Sub

  Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
      Array(0)= TextBox1.Text 'Not showing
  End Sub
Posted
Updated 1-Mar-19 21:23pm

1 solution

That's because the KeyPress event happens before the text is changed. If you think about it, that's the only way it could work: the key that he pressed might not be a text key, it could be TAB, ENTER, a cursor movement, a Function key, ... or you could be trying to prevent alphabetic keys from working in a numeric only textbox say.
The KeyPressEventArgs has the KeyChar property which tells you what key was pressed, and the Handled property to allow you to prevent the key from going any further - if it;s set to true, then it stops there and the "normal" key processing doesn't happen.
 
Share this answer
 
Comments
Maciej Los 3-Mar-19 10:57am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900