我现在要用到一个Timer
这个timer要自己定义出来
就向这样 dim mt = new Timer
但是死活就是不行
有什么办法可以做到?

解决方案 »

  1.   

    我这里正好有一个: 
    Option Base 0
    Option Explicit
    DefLng A-N, P-Z
    DefBool OPrivate mEnabled As Boolean
    Private mInterval As Long
    Private mStart As Long
    Private mName As String
    Public Event OnTimer()Private Declare Function GetTickCount Lib "kernel32" () As LongPublic Property Get Enabled() As Boolean
    Enabled = mEnabled
    End PropertyPublic Property Let Enabled(ByVal vEnabled As Boolean)
    mEnabled = vEnabled
    mStart = 0
    Call Running
    End PropertyPublic Property Let Interval(vInterval As Long)
    If vInterval > 0 Then
      mStart = 0
      mInterval = vInterval
      Call Running
    Else
      mInterval = 0
      mStart = 0
      mEnabled = False
    End If
    End PropertyPublic Property Get Interval() As Long
    Interval = mInterval
    End PropertyPublic Property Let Name(vName As String)
    If vName <> "" Then
      mName = vName
    End If
    End PropertyPublic Property Get Name() As String
    Name = mName
    End Property
    Private Sub Running()
    Dim Elapsed As LongDo While mEnabled
      If mStart = 0 Then
        mStart = GetTickCount
      End If
      Elapsed = GetTickCount
      If (Elapsed - mStart) >= mInterval Then
        mStart = GetTickCount
        RaiseEvent OnTimer
      End If
      DoEvents
    LoopEnd SubPrivate Sub Class_Initialize()
    mEnabled = False
    mInterval = 1000
    mName = "ArielTimer"
    End SubPrivate Sub Class_Terminate()
    mEnabled = False
    End Sub
      

  2.   

    把以上代码放在一个类模块里,生成这个类的实例以后,用法跟vb自带的timer控件基本一样。
      

  3.   

    其实我想做的就是模拟java中的线程,用timer实现run方法
      

  4.   

    Option ExplicitPrivate WithEvents theTimer As TimerPrivate Sub Form_Load()
        Set theTimer = Me.Controls.Add("VB.Timer", "tmrTest")
        theTimer.Interval = 1000
        theTimer = True
    End SubPrivate Sub theTimer_Timer()
        Debug.Print Timer
    End Sub
      

  5.   

    请参考:http://blog.csdn.net/Modest/archive/2006/10/23/1346175.aspx
    有完整源代码