请教:
 我是这样定义的
Dim Timer2 As Timer
Timer2.Enabled = "True"
Timer2.Interval = 2000
一直提示我 “object variable or with block variable not set ”

解决方案 »

  1.   

    这样试试:
    Option ExplicitPrivate WithEvents iTimer As TimerPrivate Sub Form_Load()
        Set iTimer = Form2.Controls.Add("VB.Timer", "miTimer")
        iTimer.Enabled = True
        iTimer.Interval = 2000
    End Sub
    以上是Form1上的代码,在Form2上动态添加一个Timer控件miTimer,Form2不用Load
      

  2.   

    或者在模块中:
    Sub main()
        Form1.Controls.Add "VB.Timer", "miTimer"
        With Form1!miTimer
            .Enabled = True
            .Interval = 2000
        End With
    End Sub
    Form1不用加载
      

  3.   

    Timer作为ActiveX控件必须加入容器中才能使用,没有Form和其他容器对象(如UserControl等)应该不行。皮之不存,毛将焉附?
      

  4.   

    set Timer2 = new Timer
      

  5.   

    直接使用API:CreateTimerQueueTimer,不需要FORM的。
      

  6.   

    API:SetTimer() & KillTimer
      

  7.   

    Dim Timer2 As Timer
    Timer2.Enabled = "True"
    Timer2.Interval = 2000“object variable or with block variable not set ”
    --------------------
    那是因为没有实例化Timer,只有实例化对象了才能使用它
    而Timer控件实例必须加入容器中才起作用
      

  8.   

    非form情况下如何定义timer控件如何实现定时中断 ?
    -------------------------个人认为,非FORM情况下,最好用SLEEP延时函数代替TIMER,并可通过检测键盘状态随时终止:如下面代码将打开记事本,在编辑窗口依次键入0+1+2+...,按左SHIFT 键终止键入,楼主试试:Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Const VK_LSHIFT = &HA0Sub main()
    Dim ReturnValue, I
    ReturnValue = Shell("notepad.exe", 1)
    AppActivate ReturnValue
    SendKeys "0", True
    For I = 1 To 20
       SendKeys "{+}" & I, True
       Sleep 500
       'DoEvents
       If GetAsyncKeyState(VK_LSHIFT) Then Exit For
    Next I
    SendKeys "= ?", TrueEnd Sub
      

  9.   

    稍改一下,动态效果更好一些:Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Const VK_LSHIFT = &HA0Sub main()
    Dim ReturnValue, I
    Dim sums As Integer
    sums = 0
    ReturnValue = Shell("notepad.exe", 1)
    AppActivate ReturnValue
    SendKeys "0", True
    For I = 1 To 200
       SendKeys "{+}" & I, True
       sums = sums + I
       Sleep 50
       'DoEvents
       If GetAsyncKeyState(VK_LSHIFT) Then Exit For
    Next I
    SendKeys "=" & sums, TrueEnd Sub