vb 怎么用一个按钮控制另一个按钮重复点击执行?
比如这一句里,我希望点击“ToggleButton2_Click()”后,自动循环点击“CMD_fireall”再次点击“ToggleButton2_Click()””后...
比如这一句里,我希望点击“ToggleButton2_Click()”后,自动循环点击“CMD_fireall”
再次点击“ToggleButton2_Click()””后,停止自动循环。
Private Sub ToggleButton2_Click()
Controls("CMD_fireall").Value = True
End Sub 展开
再次点击“ToggleButton2_Click()””后,停止自动循环。
Private Sub ToggleButton2_Click()
Controls("CMD_fireall").Value = True
End Sub 展开
4个回答
展开全部
最简单的办法加一个timer1控件,代码如下:
Private Sub Form_Load()
Timer1.Enabled = False '初始timer1的状态为未激活
Timer1.Interval = 1000 '初始timer1代码的执行周期为1000毫秒
End Sub
Private Sub Timer1_Timer()
CMD_fireall_Click
End Sub
Private Sub ToggleButton2_Click()
Timer1.Enabled = Not Timer1.Enabled
End Sub
如果不用timer控件也可以写成
Dim TmpState As Boolean
Private Sub Form_Load()
TmpState = False
End Sub
Private Sub ToggleButton2_Click()
TmpState = Not TmpState
LoopClick
End Sub
Private Sub LoopClick()
While TmpState = True
DoEvents
CMD_fireall_Click
Wend
End Sub
但是这样循环点击就是没有间隔的快速循环,如果需要间隔时间就写成:
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Dim TmpState As Boolean
Private Sub Form_Load()
TmpState = False
End Sub
Private Sub ToggleButton2_Click()
TmpState = Not TmpState
LoopClick
End Sub
Private Sub LoopClick()
Dim TimeA As Long
TimeA = timeGetTime
While TmpState = True
DoEvents
If timeGetTime - TimeA >= 1000 Then ‘设置间隔时间为1000毫秒
CMD_fireall_Click
TimeA = timeGetTime
End If
Wend
End Sub
Private Sub Form_Load()
Timer1.Enabled = False '初始timer1的状态为未激活
Timer1.Interval = 1000 '初始timer1代码的执行周期为1000毫秒
End Sub
Private Sub Timer1_Timer()
CMD_fireall_Click
End Sub
Private Sub ToggleButton2_Click()
Timer1.Enabled = Not Timer1.Enabled
End Sub
如果不用timer控件也可以写成
Dim TmpState As Boolean
Private Sub Form_Load()
TmpState = False
End Sub
Private Sub ToggleButton2_Click()
TmpState = Not TmpState
LoopClick
End Sub
Private Sub LoopClick()
While TmpState = True
DoEvents
CMD_fireall_Click
Wend
End Sub
但是这样循环点击就是没有间隔的快速循环,如果需要间隔时间就写成:
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Dim TmpState As Boolean
Private Sub Form_Load()
TmpState = False
End Sub
Private Sub ToggleButton2_Click()
TmpState = Not TmpState
LoopClick
End Sub
Private Sub LoopClick()
Dim TimeA As Long
TimeA = timeGetTime
While TmpState = True
DoEvents
If timeGetTime - TimeA >= 1000 Then ‘设置间隔时间为1000毫秒
CMD_fireall_Click
TimeA = timeGetTime
End If
Wend
End Sub
追问
我把这个加进代码里,测试的时候出现“运行时错误424需要对象”的提示,
选择调试,显示的是Timer1.Enabled = Not Timer1.Enabled这句。为什么啊。
我昨天刚开始自学VB,很笨,多多包含啊~
追答
没事的,那这样我给你详细的代码,等等哦
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Dim n As Long
Dim TmpState As Boolean
Private Sub CMD_fireall_Click() ’CMD_fireall里面的点击事件,每点击一次窗体标题栏就会自增1
n = n + 1
Me.Caption = n
End Sub
Private Sub Form_Load()
TmpState = False
End Sub
Private Sub ToggleButton2_Click()
TmpState = Not TmpState
LoopClick
End Sub
Private Sub LoopClick()
Dim TimeA As Long
TimeA = timeGetTime
While TmpState = True
DoEvents
If timeGetTime - TimeA >= 1000 Then ‘这里设置循环间隔时间,单位是毫秒
CMD_fireall_Click
TimeA = timeGetTime
End If
Wend
End Sub
用这段代码就可以在你的里面直接运行了
展开全部
很简单啊,通过一个变量来控制啊,比如定义一个变量a,当button2点一次的时候就变化一次,具体代码可以这样写:
dim a as integer
a=1(在申明中定义就行)
Private Sub ToggleButton2_Click()
if(a=1) then
Controls("CMD_fireall").Value = True
a=2
elseif(a=2)
Controls("CMD_fireall").Value = False
a=1
end if
End Sub
就是这个意思,通过这个变量去控制,然后每次修改变量名,就实现了这种点一次循环,再点一次停止的效果
dim a as integer
a=1(在申明中定义就行)
Private Sub ToggleButton2_Click()
if(a=1) then
Controls("CMD_fireall").Value = True
a=2
elseif(a=2)
Controls("CMD_fireall").Value = False
a=1
end if
End Sub
就是这个意思,通过这个变量去控制,然后每次修改变量名,就实现了这种点一次循环,再点一次停止的效果
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Timer1_Timer()
Controls("Command1").Value = True
End Sub
Private Sub ToggleButton2_Click()
If Timer1.Enabled = False Then
Timer1.Enabled = True
Else
Timer1.Enabled = False
End If
End Sub
Private Sub Command1_Click()
Me.Caption = Val(Me.Caption) + 1
End Sub
加个timer吧 就简单了
Controls("Command1").Value = True
End Sub
Private Sub ToggleButton2_Click()
If Timer1.Enabled = False Then
Timer1.Enabled = True
Else
Timer1.Enabled = False
End If
End Sub
Private Sub Command1_Click()
Me.Caption = Val(Me.Caption) + 1
End Sub
加个timer吧 就简单了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用if判断 如果在响应ToggleButton2_Click()就停止该事件 不然就执行这个事件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询