动态对象上的时间 [英] timerents on a dynamic object

查看:75
本文介绍了动态对象上的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试理解使用timer_tick事件的概念时遇到了很多麻烦,并且当这些发件人是动态的时候有发件人。



我可以得到一个用于处理表单上已有对象的计时器,例如(简单)

dim minutes为整数

textbox1.backcolor = red

如果分钟> 15然后textbox1.backcolor = color.white

else textbox1.backcolor = color.red



表单加载时,计时器启动勾选间隔设置...我相信你明白了。



我的问题是,文本框是动态创建的。



我正在使用textbox.tag来设置时间。一切正常,但是:



我不知道如何在每个文本框中启动单独的计时器,并让那些不同的计时器运行一个事件处理程序。



我想,唯一的方法是每个文本框实例一个新的计时器。但是,如何为这些单独的文本框设置timer_tick事件呢?我可以看到当我双击放在表单上的计时器对象时如何创建事件,但我猜我还需要动态设置timer_tick事件。



我不确定的另一部分是我在哪里实际倒计时?如果我将sender.tag发送到tick_event,它将在第一次运行时起作用,因为实例文本框通过addressof timer_tick调用该事件,但在第二个tick上,因为它是一个事件本身,它提供了一个对象参考错误我可以理解,timer_tick事件是发件人,它没有.tag属性



真的很困惑。



我不期待找到如何在不同的线程上设置这些东西......我的伤害开始受到伤害。

解决方案

看看这个与你的情况类似的例子:多个计时器与不同的网络间隔 [ ^ ]

在vb.net中采用这个想法。


 私有  Sub  Form1_Load(发件人 对象,e  As  EventArgs)句柄  MyBase  .Load 
Dim timer 作为 计时器
Dim textbox 作为 TextBox
.Controls.Add(文本框)
timer.Interval = 100
timer.Tag = textbox
AddHandler timer.Tick, AddressOf timer_Tick
timer.Start()
结束 < span class =code-key单词> Sub

私人 Sub timer_Tick(发件人< span class =code-keyword>作为 对象,e As EventArgs)
Dim t 作为 Timer = DirectCast (发件人,计时器)
DirectCast (t.Tag,TextBox).Text = Now.ToString( HH:mm:ss
结束 Sub





和我一起工作.. Tick事件处理程序中的发送者对象总是来电计时器。

+发送者对象本身没有标签属性..你需要把它投射到你的特定类型..在这种情况下,计时器


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.

解决方案

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.


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


这篇关于动态对象上的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆