定时器没有停止?
Timer is not stopping?
我有一个计时器,它在 5 秒后将文本框文本设置为 vbnullstring
,这是为了防止用户输入,因为他们要做的是扫描条形码,现在在读取条形码后,扫描仪会执行回车键,所以我有这个代码
'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
我的代码中没有任何会再次触发按键事件的内容,但即使按下回车键,文本框上的文本仍会被删除。
a timer that sets a textbox text to vbnullstring
after 5 seconds, this is to prevent the user from typing
为什么不直接将控件设置为只读模式?
TextBox.ReadOnly Property - MSDN - Microsoft
我将逻辑移至自定义用户控件:
''' <summary>
''' Class TextBoxEx.
''' </summary>
Public NotInheritable Class TextBoxEx : Inherits TextBox
''' <summary>
''' The delay Timer.
''' </summary>
Private WithEvents tmrDelay As Timer
''' <summary>
''' Initializes a new instance of the <see cref="TextBoxEx"/> class.
''' </summary>
Public Sub New()
Me.tmrDelay = New Timer
End Sub
''' <summary>
''' Puts the control in ReadOnly state after the specified delay interval.
''' </summary>
''' <param name="delay">The delay, in milliseconds.</param>
Public Sub SetDelayedReadonly(ByVal delay As Integer)
With Me.tmrDelay
.Interval = delay
.Enabled = True
.Start()
End With
End Sub
''' <summary>
''' Handles the Tick event of the <see cref="tmrDelay"/> instance.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) _
Handles tmrDelay.Tick
MyBase.ReadOnly = True
With Me.tmrDelay
.Stop()
.Enabled = False
End With
End Sub
End Class
然后,使用它:
Private Sub TextBoxEx1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) _
Handles TextBoxEx1.KeyDown
If e.KeyCode = Keys.Enter Then
DirectCast(sender, TextBoxEx).SetDelayedReadonly(delay:=5000)
End If
End Sub
编辑:代码已更新,我理解错了目的。
我发现你的代码有些问题
首先,按键事件按以下顺序触发
KeyDown
KeyPress
KeyUp
这意味着在您第一次启动计时器后,计时器将永远不会结束,因为您在 KeyDown
事件中停止了计时器,之后又在 [=] 中再次启动计时器12=]事件。
其次,您在没有检查计时器是否停止的情况下启动计时器。
如果你想在按下任何键时启动计时器,也许你可以在 KeyDown
事件中使用此代码
If e.KeyCode = Keys.Enter Then
Timer1.Stop()
Else If Timer1.Enabled == False Then
Timer1.Start()
End if
希望对您有所帮助。
我有一个计时器,它在 5 秒后将文本框文本设置为 vbnullstring
,这是为了防止用户输入,因为他们要做的是扫描条形码,现在在读取条形码后,扫描仪会执行回车键,所以我有这个代码
'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
我的代码中没有任何会再次触发按键事件的内容,但即使按下回车键,文本框上的文本仍会被删除。
a timer that sets a textbox text to
vbnullstring
after 5 seconds, this is to prevent the user from typing
为什么不直接将控件设置为只读模式?
TextBox.ReadOnly Property - MSDN - Microsoft
我将逻辑移至自定义用户控件:
''' <summary>
''' Class TextBoxEx.
''' </summary>
Public NotInheritable Class TextBoxEx : Inherits TextBox
''' <summary>
''' The delay Timer.
''' </summary>
Private WithEvents tmrDelay As Timer
''' <summary>
''' Initializes a new instance of the <see cref="TextBoxEx"/> class.
''' </summary>
Public Sub New()
Me.tmrDelay = New Timer
End Sub
''' <summary>
''' Puts the control in ReadOnly state after the specified delay interval.
''' </summary>
''' <param name="delay">The delay, in milliseconds.</param>
Public Sub SetDelayedReadonly(ByVal delay As Integer)
With Me.tmrDelay
.Interval = delay
.Enabled = True
.Start()
End With
End Sub
''' <summary>
''' Handles the Tick event of the <see cref="tmrDelay"/> instance.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) _
Handles tmrDelay.Tick
MyBase.ReadOnly = True
With Me.tmrDelay
.Stop()
.Enabled = False
End With
End Sub
End Class
然后,使用它:
Private Sub TextBoxEx1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) _
Handles TextBoxEx1.KeyDown
If e.KeyCode = Keys.Enter Then
DirectCast(sender, TextBoxEx).SetDelayedReadonly(delay:=5000)
End If
End Sub
编辑:代码已更新,我理解错了目的。
我发现你的代码有些问题
首先,按键事件按以下顺序触发
KeyDown
KeyPress
KeyUp
这意味着在您第一次启动计时器后,计时器将永远不会结束,因为您在 KeyDown
事件中停止了计时器,之后又在 [=] 中再次启动计时器12=]事件。
其次,您在没有检查计时器是否停止的情况下启动计时器。
如果你想在按下任何键时启动计时器,也许你可以在 KeyDown
事件中使用此代码
If e.KeyCode = Keys.Enter Then
Timer1.Stop()
Else If Timer1.Enabled == False Then
Timer1.Start()
End if
希望对您有所帮助。