I have a timer that sets a textbox text to vbnullstring
after 5 seconds, this is to prevent the user from typing since what they have to do is scan a barcode, now after reading the barcode, the scanner will do an enter key so I have this code
'TextBox Keypress event
Timer1.Start()
'TextBox keydown event
If e.KeyCode = Keys.Enter Then
Timer1.Stop()
Timer1.Dispose() 'Tried adding this but still doesn't work
End if
I don't have anything on my code that would make the keypress event fire again but even after pressing enter key text on the textbox is still removed.
And why don't just set the control to readonly mode?
TextBox.ReadOnly Property - MSDN - Microsoft
I moved the logic to a custom user-control:
Then, to use it:
EDIT: Code updated, I understood wrong the purpose.
I see some problems in your code
First, the key events are triggered in the following order
KeyDown
KeyPress
KeyUp
That means that after the first time you start the timer, the timer will not end never because you are stopping the timer in the
KeyDown
event and after that you are starting the timer again in theKeyPress
event.Second, you are starting the timer without check if the timer is stopped or not.
if you want to start the timer when any key is pressed maybe you can use this code in the
KeyDown
eventHope this helps.