把窗体卸载干净

  • 来源: 互联网 作者: 若水   2008-03-17/16:21
  • VB的End语句并不总是将在程序中打开的东西卸载得一干二净。例如你在程序中打开了一个文件,而没有用Close语句关闭这个文件,这时你通过程序中的End语句结束了程序,Windows就会认为你打开的文件正在被一个程序所使用,导致你在资源管理器中无法删除该文件。在有些情况下,如果你只用End语句来结束程序,会导致一些非常严重的后果,例如Windows会发出错误的信息,告诉你C盘损坏等等。

        因此最好是自己编写一个关闭子程序:

    Public Sub Shutdown(Optional ByVal Force As Boolean = False)

    Dim I As Long

    On Error Resume Next

    For I = Forms.Count - 1 to 0 Step -1

    Unload Forms(I) ' Triggers QueryUnload and Form_Unload

    ' If we aren't in Force mode and the

    ' unload failed, stop the shutdown.

    If Not Force Then

    If Forms.Count > I then

    Exit Sub

    End If

    End If

    Next I

    ' If we are in Force mode OR all

    ' forms unloaded, close all files.

    If Force Or (Forms.Count = 0) Then Close

    ' If we are in Force mode AND all

    ' forms not unloaded, end.

    If Force Or (Forms.Count > 0) Then End

    End Sub


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多