[VB.NET]有关控件组的问题!

前端之家收集整理的这篇文章主要介绍了[VB.NET]有关控件组的问题!前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有关控件组的问题! Private Timer_BaoJing(3) As System.Timers.Timer Private lblAd(3) As Label 我就是想把这两个控件一一对应起来 在timer的事件里操作label,Private Sub Form1_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load For i = 0 To 3 lblAd(i) = New Label lblAd(i).TextAlign = ContentAlignment.MiddleCenter lblAd(i).Top = 100 ''随便给的位置 lblAd(i).Left = 100 ''随便给的位置 lblAd(i).Visible = True lblAd(i).AutoSize = False lblAd(i).Name = i lblAd(i).Text = i lblAd(i).Show() Me.Controls.Add(lblAd(i)) Next ''********************************************************** For i = 0 To 2 Timer_BaoJing(i) = New System.Timers.Timer Timer_BaoJing(i).AutoReset = True AddHandler Timer_BaoJing(i).Elapsed,AddressOf BaoJing_Timer Timer_BaoJing(i).Interval = 1000 * i + 3000 Timer_BaoJing(i).Enabled = True Next End Sub Private Sub BaoJing_Timer(ByVal sender As Object,ByVal e As System.Timers.ElapsedEventArgs) For i = 0 To 2 If sender.Equals(Timer_BaoJing(i)) Then a = i lblAd(i).BackColor = Color.Blue ''到这里就出错了,线程之间的问题 End If Next End Sub __________________________________________________________________________ 请问怎么在这个timer的事件里控制别的控件!??? __________________________________________________________________________ Public Class Form1 Private Timer_BaoJing(3) As System.Timers.Timer Private lblAd(3) As Label Private i As Integer Private Sub Form1_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load For i = 0 To 3 lblAd(i) = New Label lblAd(i).TextAlign = ContentAlignment.MiddleCenter lblAd(i).Top = 100 * i ''随便给的位置 lblAd(i).Left = 100 ''随便给的位置 lblAd(i).Visible = True lblAd(i).AutoSize = False lblAd(i).Name = i lblAd(i).Text = i lblAd(i).Show() Me.Controls.Add(lblAd(i)) Next ''********************************************************** For i = 0 To 2 Timer_BaoJing(i) = New System.Timers.Timer Timer_BaoJing(i).AutoReset = True AddHandler Timer_BaoJing(i).Elapsed,AddressOf BaoJing_Timer Timer_BaoJing(i).Interval = 1000 * i + 1000 Timer_BaoJing(i).Enabled = True Next End Sub Private Sub BaoJing_Timer(ByVal sender As Object,ByVal e As System.Timers.ElapsedEventArgs) For i = 0 To 2 If sender.Equals(Timer_BaoJing(i)) Then ''a = i lblAd(i).BackColor = Color.Blue ''到这里就出错了,线程之间的问题 End If Next End Sub End Class 代码没什么大问题,定义一下变量i,屏蔽无用代码a=i后 可以正常运行啊。 __________________________________________________________________________ 如果有线程之间的问题,可以使用委托的方式,如: Public Class Form1 Private Timer_BaoJing(3) As System.Timers.Timer Private lblAd(3) As Label Private i As Integer Private Sub Form1_Load(ByVal sender As Object,ByVal e As System.Timers.ElapsedEventArgs) For i = 0 To 2 If sender.Equals(Timer_BaoJing(i)) Then ''a = i ''lblAd(i).BackColor = Color.Blue ''到这里就出错了,线程之间的问题 Me.Invoke(New MySub(AddressOf ChangeBackColor),i) ''使用委托的方法 End If Next End Sub Private Delegate Sub MySub(ByVal i As Integer) Private Sub ChangeBackColor(ByVal i As Integer) lblAd(i).BackColor = Color.Blue End Sub End Class __________________________________________________________________________ 用select case sender.tabindex end select 这样的选择结构 __________________________________________________________________________ 原文链接:https://www.f2er.com/vb/263838.html

猜你在找的VB相关文章