动态文本框上的定时事件 [英] Timed events on dynamic textbox

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

问题描述

我做了尽可能多的研究,但我无法理解如何让计时器正常工作。我希望,如果我为我所拥有的内容发布代码,有人可以告诉我如何编写计时器类来做他们需要做的事情。



I've done as much research as I can, but I can't wrap my head around how to get timers to work correctly. I was hoping, if I post the code up for what I have, could someone please tell me how to write the timer class to do what it needs to do.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       tcplisten.MyTcpListener.Main()
       Dim name As String
       Dim tb As New TextBox()
       Text = tcplisten.datastring
       ' set the textbox dimensions being created
       name = ("TxtBox" & CStr(counttotal))
       With tb
           .Name = name
           .Text = Text
           .Multiline = True
           .Height = 150
           .Width = 250
           .ReadOnly = True
           .ScrollBars = False
           .MaxLength = 100
           .Tag = 20
           .BackColor = Color.Black
           .ForeColor = Color.White
           '           move unused properties under here'
           '           tb.ScrollBars = ScrollBars.Both
           '           tb.MaxLength = 100
           '           tb.Height = 100
           '           tb.AutoSize = True
           ' add events to the textbox created
           FlowLayoutPanel1.Controls.Add(tb)
           Me.Show()
           AddHandler tb.MouseClick, AddressOf TextBox1_MouseClick
       End With
       count = (count + counttotal)
       ' this defines where the textboxes appear
   End Sub




Private Sub TextBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        FlowLayoutPanel1.Controls.Remove(sender)
    End Sub







我需要的是每次创建一个文本框时,它需要它自己的倒计时事件,当click_click1被点击相隔5秒创建3个单独的文本框时,textbox 1将最多15秒,textbox2将最多10秒,textbox3将是5秒。当与与timer1相关的文本框*文本框1相关的计时器(与秒2相关的文本框2)上的倒计时达到(30秒)时,文本框的前部/后部颜色发生变化。我正在使用textbox.tag来实现时间。



我不认为我想做的事情很复杂,但因为我只是新的(我已经学习了大约6个月)我仍然不确定如何正确搜索我想要的东西,所以谷歌也不是很友好。



如果有人确实发了一些东西,请问我们如何解释你的实施方式,因为我希望能够理解这一点...



我我已经尝试在表单上放一个计时器,双击它以便创建tickevent,然后我尝试将我的if语句更改为sender.color,但问题是第一个发件人工作,因为按钮单击是发送者,但在计时器的第二个刻度线上(因为它现在已经启动并且每次它打勾,这是一个事件),计时器成为事件发送者,并且sh * t变得混乱。我可以理解为什么,但我无法弄清楚该做什么。




What I need is each time a textbox is created, it needs it's own countdown event, when the button_click1 is clicked 5 seconds apart to create 3 separate textboxes, textbox 1 would be upto 15 seconds, textbox2 would be upto 10 seconds, textbox3 would be 5 seconds. When the countdown is reached (30 seconds) on the timer related to the textbox *textbox1 related to timer1, textbox2 related to timer2*, the forecolour/backcolour of the textbox changes. I am using the textbox.tag to implement the time.

I don't think what I'm trying to do is complicated, but as I am only new (I've been learning for about 6 months) I am still not sure how to search correctly for what I want, so google is not being very friendly either.

If someone does post something, could I please ask for a little explanation as to how your implementation would work, as I want to be able to understand this...

I have tried putting in a timer on the form, double clicking on that so the tickevent is created, and then I've tried to put my if statement to change the sender.color there, but the issue is the first sender works, as the button click is the sender, but on the second tick of the timer(As it's now been started and everytime it ticks, it's an event), the timer becomes the event sender, and sh*t goes haywire. I can understand why, but I can't figure out what to do.

推荐答案

你可以使用单个Timer来做到这一点。将Timer放在表单上,​​设置Interval 1000ms并启用为true。



添加字典(Of TextBox,Integer) - 我们将它用于TextBoxes的存储时间:

You can do that using single Timer. Put Timer on your form, set Interval 1000ms and Enable to true.

Add Dictionary(Of TextBox, Integer) - we will use it for store time of TextBoxes:
Private textBoxesTime As Dictionary(Of TextBox, Integer)





添加FormLoad事件处理程序以初始化Dictionary:



Add FormLoad event handler to initialize Dictionary:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    textBoxesTime = New Dictionary(Of TextBox, Integer)
End Sub





添加Timer.Tick事件处理程序:



Add Timer.Tick event handler:

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    ' Increment time of every text box
    Dim textBoxes As List(Of TextBox)
    textBoxes = textBoxesTime.Keys.ToList()
    For Each textBox In textBoxes
        ' Increase time for each TextBox
        textBoxesTime(textBox) = textBoxesTime(textBox) + 1
        ' Check if TB reached 30 secs
        If textBoxesTime(textBox) = 30 Then
            textBox.BackColor = Color.Green
        End If
    Next
End Sub





修改你的TextBox1_MouseClick方法:



Modify your TextBox1_MouseClick method:

Private Sub TextBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    flowLayoutPanel1.Controls.Remove(sender)
    textBoxesTime.Remove(sender)
End Sub





最后修改你的Button1_Click方法,在创建后将TextBox添加到字典:



And finally modify your Button1_Click method to add TextBox to dictionary after creation:

...
AddHandler tb.MouseClick, AddressOf TextBox1_MouseClick
textBoxesTime.Add(tb, 0)  <-- This line is important
...





这应该有效:)



This should work :)


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

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