vb.net Timer with index

Posted on 2013-09-11
Medium Priority
Last Modified: 2013-09-17
Hello there,
how can I have a timer with index?

something like timer(1).enabled = true
Question by:XK8ER
LVL 64

Expert Comment

by:Fernando Soto
ID: 39483665

You can create a array of timer's something like the following.

Dim t1 As New System.Windows.Forms.Timer
Dim t2 As New System.Windows.Forms.Timer

Dim timers(1) As System.Windows.Forms.Timer
timers(0) = t1
timers(1) = t2

timers(1).Enabled = True

Open in new window


Author Comment

ID: 39485085
I was thinking maybe something like this?
but how would I call the timer with index from (Handles allTime.Tick)

Private Structure myTimers
     Dim allTime As Timer
End Structure

Dim superTimer() As myTimers

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      For Index = 1 To 101
             ReDim superTimer(Index)
      Next Index

End Sub

Open in new window

LVL 86

Accepted Solution

Mike Tomlinson earned 1000 total points
ID: 39485489
You can do something like this:
Public Class Form1

    Private AllTimers As New List(Of Timer)

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 1 To 10
            Dim tmr As New Timer
            tmr.Interval = 1000
            AddHandler tmr.Tick, AddressOf tmr_Tick

            ' ... "tmr" has NOT been started at this pont! ...

    End Sub

    Private Sub tmr_Tick(sender As Object, e As System.EventArgs)
        Dim tmr As Timer = DirectCast(sender, Timer)
        ' ... do something with "tmr" ...
    End Sub

End Class

Open in new window

*But why do you need so many timers?...
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39485491
**The first Timer in "AllTimers" is at Index 0 (Zero).

Author Comment

ID: 39485526
' ... "tmr" has NOT been started at this pont! ...

how can I enable lets say Timer(5) from a button ?
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39488347

LVL 14

Assisted Solution

frankhelk earned 1000 total points
ID: 39495849
And just another way to implement the array, along with some access to the timers:
Dim i As Integer
Dim Timers As ArrayList = New ArrayList()

For i = 0 To 100

      Timers.Add(New Timer)
      Timers(i).Interval = i + 1


Open in new window


Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Native ability to set a user account password via AD GPO was removed because the passwords can be easily decrypted by any authenticated user in the domain. Microsoft recommends LAPS as a replacement and I have written an article that does something …
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
If you are looking for an automated tool which can generate reports for Outlook emails and other items from PST file, then you can go for Kernel PST Reporter tool. The reports which are created by this tool are helpful to analyze and understand PST …

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question