PC Review


Reply
Thread Tools Rate Thread

Creation of dynamic controls - Timers

 
 
Jerry Spence1
Guest
Posts: n/a
 
      20th Aug 2005
I am trying to create timers on demand by doing the following:

Dim NewTimer As New System.Timers.Timer
NewTimer.Interval = 10000
AddHandler NewTimer.Elapsed, AddressOf Timeup
NewTimer.Enabled = True
NewTimer.Start()

My problem then happens in the TimeUp routine

Normally, to get the name of the control that has been fired you would do:

Private Sub Timeup(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs)

Dim mytimer = DirectCast(sender, Control).Name.ToString

End Sub

The problem is that timers do not have a property called 'Name'. How can I
reference which timer is being fired?

Thanks

-Jerry


 
Reply With Quote
 
 
 
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      20th Aug 2005
Jerry,

Is there a special reason that you use the system.timers.timer. I like more
the simple use of the normal forms timer. Therefore I have made a sample
with that one.

It is based on just adding that Name that you want to the class. I have set
it on our website

http://www.windowsformsdatagridhelp....c-61641f5c8d9d

I hope this helps,

Cor


 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      20th Aug 2005
"Jerry Spence1" <(E-Mail Removed)> schrieb
> I am trying to create timers on demand by doing the following:
>
> Dim NewTimer As New System.Timers.Timer
> NewTimer.Interval = 10000
> AddHandler NewTimer.Elapsed, AddressOf Timeup
> NewTimer.Enabled = True
> NewTimer.Start()
>
> My problem then happens in the TimeUp routine
>
> Normally, to get the name of the control that has been fired you
> would do:
>
> Private Sub Timeup(ByVal sender As System.Object, ByVal e As
> System.Timers.ElapsedEventArgs)
>
> Dim mytimer = DirectCast(sender, Control).Name.ToString
>
> End Sub
>
> The problem is that timers do not have a property called 'Name'. How
> can I reference which timer is being fired?



In addition to Cor,

an object can be identified by it's reference. If you don't store the
reference permanently, you can't identify it later. I don't know where you
create the Timer and where you need it. If it's in a Form, store it in a
Form's field instead of in a local variable:

'outside all subs (= a field in the form):
Dim NewTimer As System.Timers.Timer

Later use the 'Is' operator to see if it's a certain timer:

If sender is NewTimer Then
'...
elseif sender is AnotherTimerIfYouHaveOne then
'...
end if


Armin

 
Reply With Quote
 
Jerry Spence1
Guest
Posts: n/a
 
      20th Aug 2005
Thanks Cor

I'm not sure what you mean by the 'normal' timer. There is a timer in the
components tab, and there is one in the forms tab - both seem to be
system.timers.timer. What's the difference?


-Jerry

"Cor Ligthert [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Jerry,
>
> Is there a special reason that you use the system.timers.timer. I like
> more the simple use of the normal forms timer. Therefore I have made a
> sample with that one.
>
> It is based on just adding that Name that you want to the class. I have
> set it on our website
>
> http://www.windowsformsdatagridhelp....c-61641f5c8d9d
>
> I hope this helps,
>
> Cor
>



 
Reply With Quote
 
Jerry Spence1
Guest
Posts: n/a
 
      20th Aug 2005
Thanks Armin

If it's reference is stored outside all Subs, then how can I create it
dynamically?

-Jerry

"Armin Zingler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Jerry Spence1" <(E-Mail Removed)> schrieb
>> I am trying to create timers on demand by doing the following:
>>
>> Dim NewTimer As New System.Timers.Timer
>> NewTimer.Interval = 10000
>> AddHandler NewTimer.Elapsed, AddressOf Timeup
>> NewTimer.Enabled = True
>> NewTimer.Start()
>>
>> My problem then happens in the TimeUp routine
>>
>> Normally, to get the name of the control that has been fired you
>> would do:
>>
>> Private Sub Timeup(ByVal sender As System.Object, ByVal e As
>> System.Timers.ElapsedEventArgs)
>>
>> Dim mytimer = DirectCast(sender, Control).Name.ToString
>>
>> End Sub
>>
>> The problem is that timers do not have a property called 'Name'. How
>> can I reference which timer is being fired?

>
>
> In addition to Cor,
>
> an object can be identified by it's reference. If you don't store the
> reference permanently, you can't identify it later. I don't know where you
> create the Timer and where you need it. If it's in a Form, store it in a
> Form's field instead of in a local variable:
>
> 'outside all subs (= a field in the form):
> Dim NewTimer As System.Timers.Timer
>
> Later use the 'Is' operator to see if it's a certain timer:
>
> If sender is NewTimer Then
> '...
> elseif sender is AnotherTimerIfYouHaveOne then
> '...
> end if
>
>
> Armin
>



 
Reply With Quote
 
Jerry Spence1
Guest
Posts: n/a
 
      20th Aug 2005
Forget my last Cor - I've just looked a bit closer. Yes they are different.

Thanks for the example you did - you're the best!

-Jerry


"Jerry Spence1" <(E-Mail Removed)> wrote in message
news:430707d9$0$17495$(E-Mail Removed)...
> Thanks Cor
>
> I'm not sure what you mean by the 'normal' timer. There is a timer in the
> components tab, and there is one in the forms tab - both seem to be
> system.timers.timer. What's the difference?
>
>
> -Jerry
>
> "Cor Ligthert [MVP]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Jerry,
>>
>> Is there a special reason that you use the system.timers.timer. I like
>> more the simple use of the normal forms timer. Therefore I have made a
>> sample with that one.
>>
>> It is based on just adding that Name that you want to the class. I have
>> set it on our website
>>
>> http://www.windowsformsdatagridhelp....c-61641f5c8d9d
>>
>> I hope this helps,
>>
>> Cor
>>

>
>



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      20th Aug 2005
"Jerry Spence1" <(E-Mail Removed)> schrieb
> Thanks Armin
>
> If it's reference is stored outside all Subs, then how can I create
> it dynamically?


You can add them to an array list.

What I don't understand: If the number of Timers is not known at design
time, the reference is already the "key" to the timer. The 'sender' is
already pointing to the timer that fired the event. Why do you need another
way of identification? I would probably understand it if the number of
Timers was determined at design time. Then you could write code
distinguishing between Timers A, B and C, but as you want to add Timers
dynamically, this is obviously not the case. So, what's your intention?


Armin

 
Reply With Quote
 
Jerry Spence1
Guest
Posts: n/a
 
      20th Aug 2005
Sorry - my knowledge of VB.Net isn't quite up to scratch to interpret what
you are saying about the 'key' to the timer.

The actual problem is that I have a number of IP based door swipes (access
control). When someone swipes a card, my program receives the card details
and processes it and opens the door (or not). It also creates a component
which receives and records an MPEG4 stream from a video source. That is all
working OK.

I also create a timer and set it to a value which is based on the length of
time that I want the video to be recorded of the person who swiped the card.
When the timer fires, it has to turn off the video stream from that
particular camera. The dynamic part is that I have no idea how many people
are likely to be swiping their cards at the same time, so I need to create
and destroy timers at will. On large installations this could be more than
20 (eg. in a college at home time). When the timer fires it will end up at a
common point, so I need to be able to identify it so that I know which video
stream to cut off.

- Jerry


"Armin Zingler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Jerry Spence1" <(E-Mail Removed)> schrieb
>> Thanks Armin
>>
>> If it's reference is stored outside all Subs, then how can I create
>> it dynamically?

>
> You can add them to an array list.
>
> What I don't understand: If the number of Timers is not known at design
> time, the reference is already the "key" to the timer. The 'sender' is
> already pointing to the timer that fired the event. Why do you need
> another
> way of identification? I would probably understand it if the number of
> Timers was determined at design time. Then you could write code
> distinguishing between Timers A, B and C, but as you want to add Timers
> dynamically, this is obviously not the case. So, what's your intention?
>
>
> Armin
>



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      20th Aug 2005
"Jerry Spence1" <(E-Mail Removed)> schrieb
> Sorry - my knowledge of VB.Net isn't quite up to scratch to
> interpret what you are saying about the 'key' to the timer.
>
> The actual problem is that I have a number of IP based door swipes
> (access control). When someone swipes a card, my program receives
> the card details and processes it and opens the door (or not). It
> also creates a component which receives and records an MPEG4 stream
> from a video source. That is all working OK.
>
> I also create a timer and set it to a value which is based on the
> length of time that I want the video to be recorded of the person
> who swiped the card. When the timer fires, it has to turn off the
> video stream from that particular camera. The dynamic part is that I
> have no idea how many people are likely to be swiping their cards at
> the same time, so I need to create and destroy timers at will. On
> large installations this could be more than 20 (eg. in a college at
> home time). When the timer fires it will end up at a common point,
> so I need to be able to identify it so that I know which video
> stream to cut off.



I don't understand the video part, but... ;-)

.... I think you are looking for an _association_ between the timer and the
video stream. You can inherit from System.Windows.Forms.Timer and add a
property referencing the video stream or whatever object you created for
this purpose.

class VideoTimer
inherits System.Windows.Forms.Timer

public readonly additionaldata as ...

public sub new(byval additionaldata as ...)
me.additionaldata = additionaldata
end sub

end class

Instead of creating a System.Windows.Forms.Timer, create an instance of your
class, and add it to the arraylist I suggested before:

dim vt as videotimer
vt = new VideoTimer(VideoStream)
al.add(vt)


If you get the Timer's event, you can cast to VideoTimer and access the data
related to it:

dim vt as videotimer
vt = directcast(sender, videotimer)
vt.additionaldata.<whatever>



Armin

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic controls creation - issue Sri Microsoft ASP .NET 1 19th Jun 2006 10:09 AM
dynamic controls creation issues =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= Microsoft ASP .NET 3 22nd Jul 2005 11:52 AM
Dynamic creation of controls in vb.net code =?Utf-8?B?SGVucnk=?= Microsoft VB .NET 2 20th Feb 2005 08:59 AM
Dynamic creation of controls Thomas Microsoft Dot NET Compact Framework 1 14th Sep 2004 07:32 PM
Bogues in IE ? : dynamic creation of controls Windows XP Internet Explorer 0 7th Jul 2004 04:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:56 PM.