只要在循环中放上DoEvents就可以了,系统会自动监测有无其他事件被触发。当你执行Unload me 时,就可退出!难道不行!
转自MSDN的例子,使用DoEvents Private Sub Command1_Click() ' Static variables are shared by all instances ' of a procedure. Static blnProcessing As Boolean Dim lngCt As Long Dim intYieldCt As Integer Dim dblDummy As Double ' When the button is clicked, test whether it's 'already processing. If blnProcessing Then ' If processing is in progress, cancel it. blnProcessing = False Else Command1.Caption = "Cancel" blnProcessing = True lngCt = 0 ' Perform a million floating-point ' multiplications. After every ' thousand, check for cancellation. Do While blnProcessing And (lngCt < 1000000) For intYieldCt = 1 To 1000 lngCt = lngCt + 1 dblDummy = lngCt * 3.14159 Next intYieldCt ' The DoEvents statement allows other ' events to occur, including pressing this ' button a second time. DoEvents Loop blnProcessing = False Command1.Caption = "Process" MsgBox lngCt & " multiplications were performed" End If End Sub
我用了DoEvents的,我的程序如下:其中加了控件listbox:list1,dirlistbox:dir1,filelistbox:file1,还有command1.在菜单project的references中家了microsoft scripting runtime,程序代码如下:Private Sub findfiles(find_name As String) Dim i, j, cnt As Single Dim fil_dri As New FileSystemObject Dim dri As Drive Dim find_name_exist As Boolean On Error Resume Next debug.print "查找新书中...(将存于NEW BOOKS的" & find_name & "书架下)" For Each dri In fil_dri.Drives If dri.DriveType = 2 Then Dim file_a As New Collection List1.Clear 'Set path to root directory Dir1.Path = dri.Path + "\" File1.Path = Dir1.Path 'Set file path 'Add files in root directory to catalog For i = 0 To File1.ListCount - If Right(File1.List(i), 3) = find_name then debug.print Dir1.Path + File1.List(i) End If Next 'Add initial subdirectories in root For i = 0 To Dir1.ListCount - 1 List1.AddItem Dir1.List(i) Next cnt = 0 'Start with first directory in list1 startpoint: Dir1.Path = List1.List(cnt) File1.Path = Dir1.Path 'Add any subdirectories found For i = 0 To Dir1.ListCount - 1 List1.AddItem Dir1.List(i) Next 'Now add files found... For i = 0 To File1.ListCount - 1 If Right(File1.List(i), 3) = find_name then debug.print Dir1.Path + File1.List(i) End If DoEvents '加了DoEvents 的 Next
'Increment count cnt = cnt + 1 If cnt < List1.ListCount Then GoTo startpoint Next End Sub
刚刚上面一个少了最下面的end if 我用了DoEvents的,我的程序如下(是一个在硬盘中查找指定扩展名的文件的程序):其中加了控件listbox:list1,dirlistbox:dir1,filelistbox:file1,还有command1.在菜单project的references中家了microsoft scripting runtime,程序代码如下:Private Sub findfiles(find_name As String) Dim i, j, cnt As Single Dim fil_dri As New FileSystemObject Dim dri As Drive Dim find_name_exist As Boolean On Error Resume Next debug.print "查找新书中...(将存于NEW BOOKS的" & find_name & "书架下)" For Each dri In fil_dri.Drives If dri.DriveType = 2 Then Dim file_a As New Collection List1.Clear 'Set path to root directory Dir1.Path = dri.Path + "\" File1.Path = Dir1.Path 'Set file path 'Add files in root directory to catalog For i = 0 To File1.ListCount - If Right(File1.List(i), 3) = find_name then debug.print Dir1.Path + File1.List(i) End If Next 'Add initial subdirectories in root For i = 0 To Dir1.ListCount - 1 List1.AddItem Dir1.List(i) Next cnt = 0 'Start with first directory in list1 startpoint: Dir1.Path = List1.List(cnt) File1.Path = Dir1.Path 'Add any subdirectories found For i = 0 To Dir1.ListCount - 1 List1.AddItem Dir1.List(i) Next 'Now add files found... For i = 0 To File1.ListCount - 1 If Right(File1.List(i), 3) = find_name then debug.print Dir1.Path + File1.List(i) End If DoEvents '加了DoEvents 的 Next
'Increment count cnt = cnt + 1 If cnt < List1.ListCount Then GoTo startpoint end if '上面少的end if Next End SubPrivate Sub command1_click() findfiles("txt") end sub 当循环还没有结束时,我按下unload me 此时,跳入下一个循环 举个例子: 当正在c盘查找时,按下unload me 则结束c盘查找,进入了d盘查找 若在按一下,则进入e盘查找,依此类推! 但是我想要求他退出所有循环,不要在查找了!怎么办?
设置一个窗体级Boolean变量,在循环中监测它,如果为真的话,则退出。 在窗体的 QueryUnload 事件中将它设为真。Option Explicit Private bExit As BooleanPrivate Sub Command1_Click() Dim n As Integer For n = 1 To 10000 If bExit Then Exit Sub Label1.Caption = n Debug.Print n DoEvents Next n End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) bExit = True End Sub
这样就没有问题了
如何在unload 是把循环停掉?
循环的检测?
:sosolong(solong) ,你的回答与我问题无关啊!
你的循环没有执行完?那你怎么来触发unload?
我以前就是这样解决的
wbdx(碳离子) :检测到了,那又怎样关闭这个循环呢?
能否给个例子
Private Sub Command1_Click()
' Static variables are shared by all instances
' of a procedure.
Static blnProcessing As Boolean
Dim lngCt As Long
Dim intYieldCt As Integer
Dim dblDummy As Double
' When the button is clicked, test whether it's
'already processing.
If blnProcessing Then
' If processing is in progress, cancel it.
blnProcessing = False
Else
Command1.Caption = "Cancel"
blnProcessing = True
lngCt = 0
' Perform a million floating-point
' multiplications. After every
' thousand, check for cancellation.
Do While blnProcessing And (lngCt < 1000000)
For intYieldCt = 1 To 1000
lngCt = lngCt + 1
dblDummy = lngCt * 3.14159
Next intYieldCt
' The DoEvents statement allows other
' events to occur, including pressing this
' button a second time.
DoEvents
Loop
blnProcessing = False
Command1.Caption = "Process"
MsgBox lngCt & " multiplications were performed"
End If
End Sub
Dim i, j, cnt As Single
Dim fil_dri As New FileSystemObject
Dim dri As Drive
Dim find_name_exist As Boolean
On Error Resume Next
debug.print "查找新书中...(将存于NEW BOOKS的" & find_name & "书架下)" For Each dri In fil_dri.Drives
If dri.DriveType = 2 Then
Dim file_a As New Collection
List1.Clear
'Set path to root directory
Dir1.Path = dri.Path + "\"
File1.Path = Dir1.Path 'Set file path
'Add files in root directory to catalog
For i = 0 To File1.ListCount -
If Right(File1.List(i), 3) = find_name then
debug.print Dir1.Path + File1.List(i)
End If
Next
'Add initial subdirectories in root
For i = 0 To Dir1.ListCount - 1
List1.AddItem Dir1.List(i)
Next
cnt = 0 'Start with first directory in list1
startpoint:
Dir1.Path = List1.List(cnt)
File1.Path = Dir1.Path
'Add any subdirectories found
For i = 0 To Dir1.ListCount - 1
List1.AddItem Dir1.List(i)
Next
'Now add files found...
For i = 0 To File1.ListCount - 1
If Right(File1.List(i), 3) = find_name then
debug.print Dir1.Path + File1.List(i)
End If
DoEvents '加了DoEvents 的
Next
'Increment count
cnt = cnt + 1
If cnt < List1.ListCount Then GoTo startpoint Next
End Sub
我用了DoEvents的,我的程序如下(是一个在硬盘中查找指定扩展名的文件的程序):其中加了控件listbox:list1,dirlistbox:dir1,filelistbox:file1,还有command1.在菜单project的references中家了microsoft scripting runtime,程序代码如下:Private Sub findfiles(find_name As String)
Dim i, j, cnt As Single
Dim fil_dri As New FileSystemObject
Dim dri As Drive
Dim find_name_exist As Boolean
On Error Resume Next
debug.print "查找新书中...(将存于NEW BOOKS的" & find_name & "书架下)" For Each dri In fil_dri.Drives
If dri.DriveType = 2 Then
Dim file_a As New Collection
List1.Clear
'Set path to root directory
Dir1.Path = dri.Path + "\"
File1.Path = Dir1.Path 'Set file path
'Add files in root directory to catalog
For i = 0 To File1.ListCount -
If Right(File1.List(i), 3) = find_name then
debug.print Dir1.Path + File1.List(i)
End If
Next
'Add initial subdirectories in root
For i = 0 To Dir1.ListCount - 1
List1.AddItem Dir1.List(i)
Next
cnt = 0 'Start with first directory in list1
startpoint:
Dir1.Path = List1.List(cnt)
File1.Path = Dir1.Path
'Add any subdirectories found
For i = 0 To Dir1.ListCount - 1
List1.AddItem Dir1.List(i)
Next
'Now add files found...
For i = 0 To File1.ListCount - 1
If Right(File1.List(i), 3) = find_name then
debug.print Dir1.Path + File1.List(i)
End If
DoEvents '加了DoEvents 的
Next
'Increment count
cnt = cnt + 1
If cnt < List1.ListCount Then GoTo startpoint
end if '上面少的end if
Next
End SubPrivate Sub command1_click()
findfiles("txt")
end sub
当循环还没有结束时,我按下unload me
此时,跳入下一个循环
举个例子:
当正在c盘查找时,按下unload me
则结束c盘查找,进入了d盘查找
若在按一下,则进入e盘查找,依此类推!
但是我想要求他退出所有循环,不要在查找了!怎么办?
在窗体的 QueryUnload 事件中将它设为真。Option Explicit
Private bExit As BooleanPrivate Sub Command1_Click()
Dim n As Integer
For n = 1 To 10000
If bExit Then Exit Sub
Label1.Caption = n
Debug.Print n
DoEvents
Next n
End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
bExit = True
End Sub
在他的提示下,我解决了问题!
给分了!
不过这儿只有20分!
另外我还发了2条同样的题目,大家可以去领分!
http://www.csdn.net/expert/topic/199/199248.shtm
另一条我也找不到了!