blasonduo
blasonduo

Reputation: 9

How do I have 1 timer for all forms which then reduces slowly? Visual Basic 2010

I have a program where you start on form 1 when a button is clicked which goes to form 2. Then it picks a random form (3,10) and stays there for ten seconds, then it goes back to form 2 to pick another random form, but this time the forms remains open for 9 and a half seconds. But everytime the random forms goes back to form 2, it NEEDS to be closed fully (me.close())

I have the random forms working going to form 2 and selecting a random form, but I can't seem to find a way for all the forms to rely on the one timer. Is it possible, is there an easier way?

Cheers!

Upvotes: 0

Views: 812

Answers (1)

Zohar Peled
Zohar Peled

Reputation: 82474

You can create a singleton class that will hold the timer inside.
Whenever a new random form is opened, the class needs to be notified with the instance of the form and the time to keep it open.
The timer will be in this class and on it's elapsed event will use the reference of the form you sent to the initialize method.

Something like this:

    Public Sub FormOpened(form As Form, CloseAfter As Integer)
        _CurrentForm = form
        _Timer.Interval = CloseAfter
        _Timer.Start()
    End Sub

    Private Sub Timer_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs)
        _CurrentForm.Close()
        _Timer.Stop()
    End Sub
End Class

Note: variables that starts with an underscore are class members.

Upvotes: 1

Related Questions