非form情况下如何定义timer控件如何实现定时中断

阿虎哥kkk 2005-11-10 03:06:44
请教:
我是这样定义的
Dim Timer2 As Timer
Timer2.Enabled = "True"
Timer2.Interval = 2000
一直提示我 “object variable or with block variable not set ”
...全文
255 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
northwolves 2005-11-10
  • 打赏
  • 举报
回复
稍改一下,动态效果更好一些:

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 = &HA0

Sub 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, True

End Sub
northwolves 2005-11-10
  • 打赏
  • 举报
回复
非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 = &HA0

Sub 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 "= ?", True

End Sub
faysky2 2005-11-10
  • 打赏
  • 举报
回复
Dim Timer2 As Timer
Timer2.Enabled = "True"
Timer2.Interval = 2000

“object variable or with block variable not set ”
--------------------
那是因为没有实例化Timer,只有实例化对象了才能使用它
而Timer控件实例必须加入容器中才起作用
WM_JAWIN 2005-11-10
  • 打赏
  • 举报
回复
API:SetTimer() & KillTimer
province_ 2005-11-10
  • 打赏
  • 举报
回复
直接使用API:CreateTimerQueueTimer,不需要FORM的。
feiyun0112 2005-11-10
  • 打赏
  • 举报
回复
set Timer2 = new Timer
winehero 2005-11-10
  • 打赏
  • 举报
回复
Timer作为ActiveX控件必须加入容器中才能使用,没有Form和其他容器对象(如UserControl等)应该不行。

皮之不存,毛将焉附?
vbman2003 2005-11-10
  • 打赏
  • 举报
回复
或者在模块中:
Sub main()
Form1.Controls.Add "VB.Timer", "miTimer"
With Form1!miTimer
.Enabled = True
.Interval = 2000
End With
End Sub
Form1不用加载
vbman2003 2005-11-10
  • 打赏
  • 举报
回复
这样试试:
Option Explicit

Private WithEvents iTimer As Timer

Private 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
fuxc 2005-11-10
  • 打赏
  • 举报
回复
没有Form,没法用Timer控件吧?

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧