实际工程中用到定时器数组控件,问题简化如下:现有定时器数组控件Timer1(0).Timer, Timer1(1).Timer......Timer1(5).Timer, 用for循环给每个数组成员interval赋值后,定时器的事件是什么呢?我用Private Sub Timer1_Timer(index As Integer),发现所有的定时器都是按时间值最小的那个来动作。通道事件也用一个for循环,应该怎么写?

解决方案 »

  1.   

    Private Sub Form_Load()
    Dim i As Byte
    For i = 0 To Timer1.Count - 1
        Timer1(i).Interval = 1000 + 1000 * i
    Next
    End SubPrivate Sub Timer1_Timer(Index As Integer)
    Debug.Print "timer" & Index
    End Sub需求不明确,看看这个效果
      

  2.   

    Private Sub Timer1_Timer(index As Integer) 中的Index是区别哪个定时器的
      

  3.   


    Dim a(5), b(5), c(5), x(5), y(5) As Integer
    Private Sub Command1_Click()    For n = 0 To 5
            Timer2(n).Interval = y(n)
            Timer2(n).Enabled = True
        Next n
    End SubPrivate Sub Command2_Click()    For n = 0 To 5
            Text1(n).Text = 0
            Text2(n).Text = 0
            Text3(n).Text = 0
        Next n
    End SubPrivate Sub Form_Load()
        
        Timer1.Interval = 50
        For n = 0 To 5
            Text1(n).Text = 0
            Text2(n).Text = 0
            Text3(n).Text = 0
        Next n
    End SubPrivate Sub Timer1_Timer()    For k = 0 To 5
            a(k) = Val(Text1(k).Text)
            b(k) = Val(Text2(k).Text)
            c(k) = Val(Text3(k).Text)
            
            x(k) = Abs((b(k) - a(k)) / 0.1)
            
            If x(k) = 0 Then
                y(k) = 0
            Else
                y(k) = c(k) * 1000 / x(k)
            End If
            
        Next k
        
    End SubPrivate Sub Timer2_Timer(Index As Integer)    For m = 0 To 5
            If b(m) > a(m) Then
                a(m) = a(m) + 0.1
            Else
                If b(m) < a(m) Then
                    a(m) = a(m) - 0.1
                Else
                    If a(m) = b(m) Then
                        Timer2(m).Enabled = False
                        
                    End If
                End If
            End If
        
            Text1(m).Text = a(m)
        Next m
        
    End Sub
      

  4.   

    text1显示的数值根据text2的数值按0.1递增或递减,直到与text2的数值相同。而递增或递减时间由text3的数值来定。比如我在text3中都输入5,text2中输入1、2、3、4、5、6. 希望text1(0)经过5秒到达1,text1(1)经过5秒到达2……,但现在的效果是数字小的先到,数字最大的经过5秒到。
    请各位朋友指教。
      

  5.   

    Private Sub Timer1_Timer(index As Integer) '中的Index是区别哪个定时器的
    select case index
    case 0
    'timer1(0)的事件
    case 1
    'timer1(1)的事件
    case 2
    'timer1(2)的事件
    case 3
    'timer1(3)的事件
    end select
      

  6.   

    Private Sub Timer2_Timer(Index As Integer)
    '
    '    For m = 0 To 5
    '        If b(m) > a(m) Then
    '            a(m) = a(m) + 0.1
    '        Else
    '            If b(m) < a(m) Then
    '                a(m) = a(m) - 0.1
    '            Else
    '                If a(m) = b(m) Then
    '                    Timer2(m).Enabled = False
    '                    
    '                End If
    '            End If
    '        End If
    '    
    '        Text1(m).Text = a(m)
    '    Next m    a(Index) = a(Index) + 0.1
        If b(Index) = a(Index) Then Timer2(Index).Enabled = False
        Text1(Index) = a(Index)
        
    End Sub
    要考虑timer的延时不很准确,最低大约20ms+-5ms
      

  7.   

    Private Sub Timer1_Timer(index As Integer) '中的Index是区别哪个定时器的
    select case index
    case 0
    'timer1(0)的事件
    case 1
    'timer1(1)的事件
    case 2
    'timer1(2)的事件
    case 3
    'timer1(3)的事件
    end select
      

  8.   

    谢谢各位朋友。先前也考虑过select case,但又考虑到如果分支多了(实际项目中分支会增加很多,但动作是一样的)。
    用7楼朋友的方法,问题解决。结帖,散分!