Timer is not stopping?

2019-09-19 01:30发布

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.

2条回答
叛逆
2楼-- · 2019-09-19 02:16

a timer that sets a textbox text to vbnullstring after 5 seconds, this is to prevent the user from typing

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:

''' <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

Then, to use it:

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

EDIT: Code updated, I understood wrong the purpose.

查看更多
Bombasti
3楼-- · 2019-09-19 02:24

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

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

Hope this helps.

查看更多
登录 后发表回答