I would like to, if cleanly possible, start a timer after a textbox length has reached a set amount of characters. The timer interval would be set at 'x' amount of seconds and would call my save feature, but I haven't really done anything with timers and not totally sure how to word this event. Could anyone give me an idea as to the approach?

Dani AI

Generated

Builds on 's question and the good pointers from (TextChanged) and (length check). A more robust pattern is a debounce: restart a short timer on each TextChanged and run your save only when the timer actually elapses. That prevents repeated saves while the user is typing and keeps the UI responsive — taking into account 's note that a Timer keeps firing until stopped.

Here is a simple WinForms pattern (VB.NET) using a dedicated timer instance:

Private WithEvents autosaveTimer As New System.Windows.Forms.Timer()
Private Const SaveThreshold As Integer = 50

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    autosaveTimer.Interval = 5000 ' 5 seconds
End Sub

Private Sub txtEditor_TextChanged(sender As Object, e As EventArgs) Handles txtEditor.TextChanged
    If txtEditor.Text.Length < SaveThreshold Then
        autosaveTimer.Stop()
        Return
    End If
    autosaveTimer.Stop()   ' debounce: restart on each change
    autosaveTimer.Start()
End Sub

Private Sub autosaveTimer_Tick(sender As Object, e As EventArgs) Handles autosaveTimer.Tick
    autosaveTimer.Stop()   ' run once, then stop
    SaveDocument()         ' call your save routine
End Sub

If you prefer to avoid any Timer object, an async debounce using Task.Delay and CancellationTokenSource is clean and avoids cross-thread issues:

Private cts As CancellationTokenSource
Private Const SaveDelayMs As Integer = 5000

Private Async Sub txtEditor_TextChanged(sender As Object, e As EventArgs) Handles txtEditor.TextChanged
    cts?.Cancel()
    If txtEditor.Text.Length < 50 Then Return
    cts = New CancellationTokenSource()
    Try
        Await Task.Delay(SaveDelayMs, cts.Token)
        Await SaveAsync()  ' perform IO off the UI thread
    Catch ex As OperationCanceledException
    End Try
End Sub

Troubleshooting notes: use System.Windows.Forms.Timer for UI-thread callbacks; if you use System.Timers.Timer or Threading.Timer, marshal back to the UI with Invoke/BeginInvoke. Never do long file I/O on the UI thread — use Task.Run or an async save. Keep a simple "isSaving" or "lastSavedText" check to avoid redundant writes, and Stop/Dispose timers (and cancel CTS) on form closing.

Recommended Answers

All 5 Replies

You'll first need to fire an event when a textbox reaches a certain length. You would want to make a sub that Handles MyTextBox.OnTextChanged and checks how many characters are in the textbox.

Then inside that sub, if the number of characters have been reached, you'll enable the timer. Timer1.Enabled = True. Ez pz. How do you make the timer? Drag and drop one onto your form. Right click on it and select properties to set the delay and if it starts enabled or not (you want false). THEN double click on the timer object you dragged onto your form and write the code you want to execute each time.

You set the timer interval (the time between tick events) via the Interval property. Set it to 1000 for a one second interval. You start the timer by setting Enabled = True.

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

    'this event fires every Timer.Interval milliseconds
    'you could do a file save here

End Sub

Check first the lenght of the characters in the textbox before you execute the timer1_tick event.

Thanks guys. I think I pretty much got it, but do have one question pertaining to the timer. Regardless of the interval it's set at i.e. 5000 milliseconds, does it repeat itself or just stop/dispose?

The timer will continue to fire every Interval milliseconds until you disable it. If you only want it to fire once then you can set Enabled = False in the Tick handler. The next time you set Enabled = True it will fire again after the interval and then disable itself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.