先把循环停掉。要不然就 end. 
但是我不建议用 end

解决方案 »

  1.   

    我认为,应该在循环时将form.enabled=false,然后在循环结束时form.enabled=true
    这样就没有问题了
      

  2.   

    end不行的,其他窗口也关了
    如何在unload 是把循环停掉?
    循环的检测?
    :sosolong(solong) ,你的回答与我问题无关啊!
      

  3.   

    我想知道的是:当unload窗体时,如何使还未执行完的循环停止?
      

  4.   

    我想问一下:
    你的循环没有执行完?那你怎么来触发unload?
      

  5.   

    我说的检测是,你在 form_queryUnload 事件中置某变量作标记 ,你在循环中检测标记退出
    我以前就是这样解决的
      

  6.   

    gameboy999(活着就是幸运) :循环中用的doevents
    wbdx(碳离子) :检测到了,那又怎样关闭这个循环呢?
      

  7.   

    以上关于在循环中加入判断只是个折中的方法(如一个按钮事件中将一boolean值设为true,在循环中判断如此值为true,跳出循环),如果你的循环是类似递归的有堆栈操作的循环时,恐怕虽然能跳出,但时间必然由堆栈当前的状况而延后,好办法是做成多线程的程序,那个循环放在一个新建的线程之内运行,点终止按钮时只要杀了那线程就可以了 建线程用api CreateThread,这是回调函数,你把循环写在那函数里就可以了.
      

  8.   

    redbirdli(火鸟) :
    能否给个例子
      

  9.   

    只要在循环中放上DoEvents就可以了,系统会自动监测有无其他事件被触发。当你执行Unload me 时,就可退出!难道不行!
      

  10.   

    转自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
      

  11.   

    我用了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
      

  12.   

    刚刚上面一个少了最下面的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盘查找,依此类推!
    但是我想要求他退出所有循环,不要在查找了!怎么办?
      

  13.   

    设置一个窗体级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
      

  14.   

    多谢大家,特别是fishzone(fish) 兄!
    在他的提示下,我解决了问题!
    给分了!
    不过这儿只有20分!
    另外我还发了2条同样的题目,大家可以去领分!
    http://www.csdn.net/expert/topic/199/199248.shtm
    另一条我也找不到了!