Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having a lot of trouble trying to understand the concept of using the timer_tick event and having senders when those senders are dynamic.

I can get a timer to work on an object that is already on the form, e.g (Simplistic)
dim minutes as integer
textbox1.backcolor = red
if minutes > 15 then textbox1.backcolor = color.white
else textbox1.backcolor = color.red

on form load, the timer starts the tick interval is set...I'm sure you understand.

My issue is, the textboxes are created dynamically.

I'm using textbox.tag to set the time. that all works, But:

I don't know how to have separate timers per textbox start, and have those different timers run an event handler.

I thought, the only way to do it would be to instance a new timer per textbox. But how do I then setup a timer_tick event for those separate textboxes? I can see how the event is created when I double click on the timer object put on the form, but I'm guessing I need to also dynamically set the timer_tick event.

The other part I'm not to sure on is where do I actually put the countdown? If I sender.tag to the tick_event it works the first run through, as the class that instances the textbox calls that event through addressof timer_tick, but on the second "tick" as it is an event itself, it comes up with an objevt reference error "Which I can understand, the timer_tick event is the sender, and it doesn't have a .tag property"

Real confused.

I'm not looking forward to finding out how to setup this stuff on separate threads...My hurt is starting to hurt.
Posted

Take a look at this example that is similar to your situation: multiple-timer-with-different-interval-in-c-net[^]
Adopt the idea in vb.net.
 
Share this answer
 
Comments
Mendaharin 12-Mar-14 8:25am    
Just had a look. Will try to adapt to my requirements... that code is damn confusing though for a beginner. Understandable but confusing
VB
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim timer As New Timer
        Dim textbox As New TextBox
        Me.Controls.Add(textbox)
        timer.Interval = 100
        timer.Tag = textbox
        AddHandler timer.Tick, AddressOf timer_Tick
        timer.Start()
   End Sub

   Private Sub timer_Tick(sender As Object, e As EventArgs)
        Dim t As Timer = DirectCast(sender, Timer)
        DirectCast(t.Tag, TextBox).Text = Now.ToString("HH:mm:ss")
   End Sub


works just fine with me.. the sender Object in the Tick Eventhandler is allways the caling Timer.
+ sender is Object is has no Tag Property itself.. you need to cast it to your specific Type.. In this case, Timer
 
Share this answer
 
Comments
Mendaharin 12-Mar-14 8:21am    
I can get that to work aswell. Very easy. But you are casting the textbox to display the time as a str. Not quite what I need. I need a countdown timer on each textbox, when that countdown is reached colours change. The first sender is from the textbox class, which assigns tag to the countdown int. But, 2nd tick the sender of the event is itself. Hence null reference object error
F. Xaver 13-Mar-14 4:26am    
as from that deleted Comment.. i would realy recommend that you use
Option Explicit On
Option Strict On
Option Infer Off
Mendaharin 13-Mar-14 5:31am    
I had posted the code but forgot my edits so it would have looked like I had no idea.. what would those options chg out of curiosity?
F. Xaver 14-Mar-14 4:09am    
they force you to write typesafe and less error-prone code..

see
http://support.microsoft.com/kb/311329
http://msdn.microsoft.com/library/bb384665.aspx

for more details
F. Xaver 12-Mar-14 8:48am    
I still don't get.. how your senders change Type... post that code?

hm, as you can't change the timer Interval.. hm
why not set the TextBox.Tag Property to the DateTime the countdown ends.
So you can just do a simple check on that.. like DirectCast(textbox.Tag, Date) > Now then change backgound and stop the timer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900