Dman
Dman

Reputation: 573

Loop through all Timers

I need to loop through all the timers on my form and check if they are enabled or not

I have found the following code: How will i Stop all timers in a form vb.net

For Each c As Object In Me.components.Components
         If TypeOf c Is Timer Then
            CType(c, Timer).Enabled = False
        End If
    Next

However it gives me an error

 .Enabled is not a member of the system.threading.timer

How can I loop through all Windows.forms.timers ?

Upvotes: 1

Views: 847

Answers (2)

David Wilson
David Wilson

Reputation: 4439

Rather than loop through every control, just loops through the timers like this.

For Each c As Timer In Me.components.Components
    If c.Enabled = False Then
        c.Enabled = True
    End If
Next

Upvotes: 2

David Tansey
David Tansey

Reputation: 6023

The problem is that the cast you are doing, CType(c, Timer) is using an unqualified type name of Timer and is subsequently getting cast to System.Threading.Timer when it should be System.Windows.Forms.Timer.

If you fully qualify the type name then it should work the way that you expect:

For Each c As Object In Me.components.Components
   If TypeOf c Is Timer Then
       CType(c, System.Windows.Forms.Timer).Enabled = False
   End If
Next

Upvotes: 4

Related Questions