By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

Identify which timer fired event?

P: n/a
I have a few timers that are added to a form at runtime. I can handle the
event fine, but I cannot identify which timer fired. Is there a way to do
this?
Timer Creation:
-------------
....some code...
Dim usersTimers(4) As System.Windows.Forms.Timer
For i = 0 To 4
'Build timer
usersTimers(i) = New System.Windows.Forms.Timer
With usersTimers(i)
'.name = "Timer" & Format(i, "00") - No NAME property on dynamic timer.
.Interval = Rand(5000) + 5000 'Five second + random amount
.Start()
End With
AddHandler usersTimers(i).Tick, AddressOf timTick
Next i
....more code...
Timer Event Handler:
-------------------
Protected Sub timTick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'Timer ticked. Handle it!
'MsgBox(sender.name) - No NAME property on a dynamic timer. How to ID?
MsgBox(sender.ToString)
End Sub
Timers don't get added to forms, so you can identify the control the
contains the timer. There is no TAG property on the timer. The only value
that I can seem to identify from the timer is it's interval. The above code
shows "[System.Windows.Forms.Timer], Interval: 100"

P.s. RAND() function generates a random integer between 0 and value,
inclusive.
Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
you could try to use Tag propery to hold a unique ID and
then read it in your timTick procedure...
-----Original Message-----
I have a few timers that are added to a form at runtime. I can handle theevent fine, but I cannot identify which timer fired. Is there a way to dothis?
Timer Creation:
-------------
....some code...
Dim usersTimers(4) As System.Windows.Forms.Timer
For i = 0 To 4
'Build timer
usersTimers(i) = New System.Windows.Forms.Timer
With usersTimers(i)
'.name = "Timer" & Format(i, "00") - No NAME property on dynamic timer. .Interval = Rand(5000) + 5000 'Five second + random amount .Start()
End With
AddHandler usersTimers(i).Tick, AddressOf timTick
Next i
....more code...
Timer Event Handler:
-------------------
Protected Sub timTick(ByVal sender As System.Object, ByVal e AsSystem.EventArgs)
'Timer ticked. Handle it!
'MsgBox(sender.name) - No NAME property on a dynamic timer. How to ID? MsgBox(sender.ToString)
End Sub
Timers don't get added to forms, so you can identify the control thecontains the timer. There is no TAG property on the timer. The only valuethat I can seem to identify from the timer is it's interval. The above codeshows "[System.Windows.Forms.Timer], Interval: 100"

P.s. RAND() function generates a random integer between 0 and value,inclusive.
.

Nov 20 '05 #2

P: n/a
Stop for a minute and THINK about what the event signature means.... I'll
wait.

Did ya see it?

If Directcast(Sender, Windows.Forms.Timer) = userTimers(0) Then
Elseif Directcast(Sender, Windows.Forms.Timer) = userTimers(1) Then
End If
The sender is actually a reference to the object that directly fired the
event. Just cast it to a timer and compare the references. Since you can be
guaranteed without fail that Sender will be a timer, then DirectCast is
faster and appropriate. Otherwise you can use CType and trap for an error.
(Also, ctype will look for various possible casts. Directcast will simply
fail if it cannot do a direct and immediate cast.)

-- russ

"Grahmmer" <po********@127.0.0.1> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I have a few timers that are added to a form at runtime. I can handle the
event fine, but I cannot identify which timer fired. Is there a way to do
this?
Timer Creation:
-------------
...some code...
Dim usersTimers(4) As System.Windows.Forms.Timer
For i = 0 To 4
'Build timer
usersTimers(i) = New System.Windows.Forms.Timer
With usersTimers(i)
'.name = "Timer" & Format(i, "00") - No NAME property on dynamic timer. .Interval = Rand(5000) + 5000 'Five second + random amount
.Start()
End With
AddHandler usersTimers(i).Tick, AddressOf timTick
Next i
...more code...
Timer Event Handler:
-------------------
Protected Sub timTick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'Timer ticked. Handle it!
'MsgBox(sender.name) - No NAME property on a dynamic timer. How to ID?
MsgBox(sender.ToString)
End Sub
Timers don't get added to forms, so you can identify the control the
contains the timer. There is no TAG property on the timer. The only value
that I can seem to identify from the timer is it's interval. The above code shows "[System.Windows.Forms.Timer], Interval: 100"

P.s. RAND() function generates a random integer between 0 and value,
inclusive.

Nov 20 '05 #3

P: n/a
There is no TAG on the timers.

"Sergey Poberezovskiy" <an*******@discussions.microsoft.com> wrote in
message news:03****************************@phx.gbl...
you could try to use Tag propery to hold a unique ID and
then read it in your timTick procedure...
-----Original Message-----
I have a few timers that are added to a form at runtime.

I can handle the
event fine, but I cannot identify which timer fired. Is

there a way to do
this?
Timer Creation:
-------------
....some code...
Dim usersTimers(4) As System.Windows.Forms.Timer
For i = 0 To 4
'Build timer
usersTimers(i) = New System.Windows.Forms.Timer
With usersTimers(i)
'.name = "Timer" & Format(i, "00") - No NAME

property on dynamic timer.
.Interval = Rand(5000) + 5000 'Five second + random

amount
.Start()
End With
AddHandler usersTimers(i).Tick, AddressOf timTick
Next i
....more code...
Timer Event Handler:
-------------------
Protected Sub timTick(ByVal sender As System.Object,

ByVal e As
System.EventArgs)
'Timer ticked. Handle it!
'MsgBox(sender.name) - No NAME property on a dynamic

timer. How to ID?
MsgBox(sender.ToString)
End Sub
Timers don't get added to forms, so you can identify the

control the
contains the timer. There is no TAG property on the

timer. The only value
that I can seem to identify from the timer is it's

interval. The above code
shows "[System.Windows.Forms.Timer], Interval: 100"

P.s. RAND() function generates a random integer between

0 and value,
inclusive.
.

Nov 20 '05 #4

P: n/a

"Russ Bishop" <nowhere> wrote in message
news:u5**************@TK2MSFTNGP09.phx.gbl...
Stop for a minute and THINK about what the event signature means.... I'll
wait.

Did ya see it?

If Directcast(Sender, Windows.Forms.Timer) = userTimers(0) Then
Elseif Directcast(Sender, Windows.Forms.Timer) = userTimers(1) Then
End If
ALMOST.... Try:
If Directcast(Sender, Windows.Forms.Timer) IS userTimers(0) Then...

But it makes perfect sense. I just thought it odd that there was no way to
tag a timer, but then again I'm not much of a VB programmer yet.
The sender is actually a reference to the object that directly fired the
event. Just cast it to a timer and compare the references. Since you can be guaranteed without fail that Sender will be a timer, then DirectCast is
faster and appropriate. Otherwise you can use CType and trap for an error.
(Also, ctype will look for various possible casts. Directcast will simply
fail if it cannot do a direct and immediate cast.)


One question... Is there any reason that I need the Directcast? The
following also works (Only timers will fire this event):
If Sender IS userTimers(0) Then...
Nov 20 '05 #5

P: n/a
"Grahammer" <po********@127.0.0.1> wrote...
If Directcast(Sender, Windows.Forms.Timer) IS userTimers(0) Then...
BTW you would probably want to assign the first DirectCast to a timer
variable to eliminate casting it for every test. Cast once, test four
times.
One question... Is there any reason that I need the Directcast? The
following also works (Only timers will fire this event):
If Sender IS userTimers(0) Then...


You should only be required to cast sender to a timer if you intend to
reference the properties of the timer.

Tom
Nov 20 '05 #6

P: n/a
If you are using Option Strict, you will get a complaint (I think...)

If it works without the cast then don't use it.

-- russ

"Grahammer" <po********@127.0.0.1> wrote in message
news:OC**************@tk2msftngp13.phx.gbl...

"Russ Bishop" <nowhere> wrote in message
news:u5**************@TK2MSFTNGP09.phx.gbl...
Stop for a minute and THINK about what the event signature means.... I'll wait.

Did ya see it?

If Directcast(Sender, Windows.Forms.Timer) = userTimers(0) Then
Elseif Directcast(Sender, Windows.Forms.Timer) = userTimers(1) Then
End If


ALMOST.... Try:
If Directcast(Sender, Windows.Forms.Timer) IS userTimers(0) Then...

But it makes perfect sense. I just thought it odd that there was no way to
tag a timer, but then again I'm not much of a VB programmer yet.
The sender is actually a reference to the object that directly fired the
event. Just cast it to a timer and compare the references. Since you can

be
guaranteed without fail that Sender will be a timer, then DirectCast is
faster and appropriate. Otherwise you can use CType and trap for an error. (Also, ctype will look for various possible casts. Directcast will simply fail if it cannot do a direct and immediate cast.)


One question... Is there any reason that I need the Directcast? The
following also works (Only timers will fire this event):
If Sender IS userTimers(0) Then...

Nov 20 '05 #7

P: n/a
"Grahmmer" <po********@127.0.0.1> schrieb
Dim usersTimers(4) As System.Windows.Forms.Timer ...more code...
Timer Event Handler:
-------------------
Protected Sub timTick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'Timer ticked. Handle it!
'MsgBox(sender.name) - No NAME property on a dynamic timer. How to
ID? MsgBox(sender.ToString)
End Sub


The sender argument points to the timer. You can compare reference by using
the Is keyword, e.g.

If sender is userTimers(0) Then
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.