Hi ,

I need help because I would lik a timer of 1 minute in one of my forms, and when that timer reaches zero, it would automaticly resume to the next form.

Any help would be greatly appreciated!

Dani AI

Generated

What trips this up most often (as in ’s Sub New example) is mixing timer instances. Declaring Dim timer1 As New Timer inside the constructor creates a second, local timer that is different from the designer’s Timer1. You then end up enabling/stopping one instance while the other is firing the Tick. Also, starting the timer in the constructor or Load can cause odd UI timing. A simple, reliable pattern is: keep one System.Windows.Forms.Timer at form scope, start it when the form is shown, and always stop it before navigating.

Example that includes a Cancel button and avoids reentrancy:

Public Class ConnectForm
    Private ReadOnly _delay As New System.Windows.Forms.Timer() With {.Interval = 5000}

    Private Sub ConnectForm_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        AddHandler _delay.Tick, AddressOf OnDelayTick
        _delay.Start()
    End Sub

    Private Sub OnDelayTick(sender As Object, e As EventArgs)
        _delay.Stop()
        RemoveHandler _delay.Tick, AddressOf OnDelayTick

        Dim nextForm As New NextForm()
        nextForm.Show()
        Me.Hide() 'If this is your startup form, Close() may exit the app.
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        _delay.Stop()
        RemoveHandler _delay.Tick, AddressOf OnDelayTick
        'Optionally show another form here, or just stay on this one.
    End Sub

    Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
        _delay.Dispose()
        MyBase.OnFormClosed(e)
    End Sub
End Class

Notes:

  • Use System.Windows.Forms.Timer for UI scenarios. System.Timers.Timer or System.Threading.Timer fire on worker threads and complicate form updates.
  • Do not use Thread.Sleep on the UI thread; it blocks the message pump and freezes the form.
  • If this form is your startup form, prefer Hide() over Close() or change the VB Application Framework shutdown mode to “When last form closes.”
  • On newer projects (.NET 4.5+), you can replace the timer with Await Task.Delay(5000) and cancel via a CancellationToken, but the WinForms timer is perfectly fine here.

I think another thread would be more apropriate but anyway...
So you have a listview and and when you clieck an item you want that item displayed on a new form in a textbox? And what about option an option button?

No nothing like that, i have a form wich says Establishing a connection, but i don't want it to make a connection, i just want the user to think its making a connection.

I have tried System.thread.sleep(5000), (or something like that)

But that was unsuccesful

The earlier post was for suhana
Anyway take the timer example from dan5150

Dim timercount As Integer = 60 'The number of seconds 
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Timer1.Interval = 1000 'The number of miliseconds in a second
        Timer1.Enabled = True 'Start the timer
    End Sub

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Timer1.Enabled = False 'Stop the timer
        timercount = 60 'Reset to 60 seconds
        lblOutput.Text = timercount.ToString() 'Reset the output display to 60
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblOutput.Text = timercount.ToString() 'show the countdown in the label
        If timercount = 0 Then 'Check to see if it has reached 0, if yes then stop timer and display done
            Timer1.Enabled = False
            lblOutput.Text = "Done"

'added by me
            Dim SecondForm as new form2
            SecondForm.show
'till here

        Else 'If timercount is higher then 0 then subtract one from it
            timercount -= 1

        End If
    End Sub

And you have to change a bit of the code to fit your needs.

actually what do you have problems with? just the timer? you dont know how to use a timer control?

Actually I don't want any buttons, i just want a timer which counts down from 5 seconds (i'm sorry for the thread title error)
and the automaticly opens the next form, and no, i don't know how to use a timer control, nor the timer

Pieter

go to the tool box and put a timer on your Form1
add another form ( the form you want to appear after 5 sec)

Class form1

    Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 5000 'sets the timer's tick interval to 5 sec
        Timer1.Enabled = True 'the timer is disabled on form load so you have to enable it


    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick  'when 5 sec have passed this starts
        Timer1.Enabled = False ' timer has done it's job so you should disable it
        Dim frm2 As New Form2 
        frm2.Show() 'this shows the second form

    End Sub
End Class

If you want the remaining time to be displayed (5,4,3,2,1).... that can be done too, i can show you how to do that too.


hope it's explicit enough but you should really start with some tutorials for the basics. You can find some on youtube that are easy to understand and well explained.

Thanks for your reply, but it's not exactly what I seek, i've got the code for the timer, but i want to make a cancel button to stop the timer.
And until now it has been unsuccessful, i have tried several things like Timer1.Dispose, and Timer1.Enabled = False , but that was unsuccessful as well.

I dont see why timer1.enable = false would not work, it should.
You can also declare a global variable in the form1 class that changes when you click the cancel button and then on the timer click method you check if that variable cntains the cancel number and not run the secondform.show
Give me the code that you are trying to use i can take a look and tell you why it isn't working. you must be doing something wrong.

Hey , I've used the following code :

Public Sub New()
        Dim timer1 As New Timer
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Timer1.Interval = 5000
        Timer1.Enabled = True
        AddHandler Timer1.Tick, AddressOf Timer1_Tick
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Enabled = False
        Hoofdmenu.Show()
        Me.Close()
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim f2 As New profiel
        f2.Show()
        CType(sender, Timer).Enabled = False
        Close()
    End Sub

I'm not sure why it doesent work but i am sure this works using the timer object.
Your code seems ok to me but i tryed it and it doesent work. But i'm not that experienced, maybe someone else can help you. But unles you can fix your code or you want to use my version you should try to create a custom made timer with a worker thread or a normal thhread that you can control. It's simple, if you wold like an example for creating a custom timer let me know.

Public Class Form1

Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load    
    Timer1.Interval = 5000  
      Timer1.Enabled = True
      End Sub   

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Timer1.Enabled = False
    
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim f2 As New profiel
        f2.Show()

        Timer1.Enabled = False

    End Sub
End Class
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.