请教:
我是这样定义的
Dim Timer2 As Timer
Timer2.Enabled = "True"
Timer2.Interval = 2000
一直提示我 “object variable or with block variable not set ”
我是这样定义的
Dim Timer2 As Timer
Timer2.Enabled = "True"
Timer2.Interval = 2000
一直提示我 “object variable or with block variable not set ”
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
Sub main()
Form1.Controls.Add "VB.Timer", "miTimer"
With Form1!miTimer
.Enabled = True
.Interval = 2000
End With
End Sub
Form1不用加载
Timer2.Enabled = "True"
Timer2.Interval = 2000“object variable or with block variable not set ”
--------------------
那是因为没有实例化Timer,只有实例化对象了才能使用它
而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
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