定时器没有停止? [英] Timer is not stopping?

查看:52
本文介绍了定时器没有停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计时器,它在 5 秒后将文本框文本设置为 vbnullstring,这是为了防止用户打字,因为他们要做的是扫描条码,现在在读取条码后,扫描仪会按回车键,所以我有这个代码

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

我的代码中没有任何会使 keypress 事件再次触发的内容,但即使在按下 Enter 键后,文本框中的文本仍会被删除.

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.

推荐答案

我发现你的代码有问题

首先按以下顺序触发按键事件
KeyDown
按键
KeyUp

First, the key events are triggered in the following order
KeyDown
KeyPress
KeyUp

这意味着在您第一次启动计时器之后,计时器不会永远结束,因为您在 KeyDown 事件中停止了计时器,然后您在 KeyDown 事件中再次启动计时器code>KeyPress 事件.

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 the KeyPress event.

其次,您在不检查计时器是否停止的情况下启动计时器.

Second, you are starting the timer without check if the timer is stopped or not.

如果您想在按下任何键时启动计时器,您可以在 KeyDown 事件中使用此代码

if you want to start the timer when any key is pressed maybe you can use this code in the KeyDown event

If e.KeyCode = Keys.Enter Then
   Timer1.Stop()
Else If Timer1.Enabled == False Then
   Timer1.Start()
End if

希望这会有所帮助.

这篇关于定时器没有停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆