首页/技术开发/内容

如何在VB中把窗体删除干净

技术开发2024-06-13 阅读()
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


……

相关阅读