新手,编程中遇到一个,我的程序中用了一个command1的按钮,用于退出程序。在退出程序前时需要做一些我需要的工作例如:
......
......
......  '这是我退出程序前需要运行的
End     '最后再退出
但是如果用户点右上角窗体的叉叉,程序就自动退出了。而达不到我的需要。请教如何才能点右上叉叉时实现我的需求?谢谢。

解决方案 »

  1.   

    Sub Form_Unload
    ......
    ......
    ......  '这是我退出程序前需要运行的
    End Sub
      

  2.   

    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        Dim i%
        For i = 0 To 3
            Debug.Print i
        Next
        MsgBox "做完上面的事就退出!"
    End Sub
      

  3.   

    在QueryUnload中处理吧
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
       Dim Msg   ' Declare variable.
       If UnloadMode > 0 Then
          ' If exiting the application.
          Msg = "Do you really want to exit the application?"
       Else
          ' If just closing the form.
          Msg = "Do you really want to close the form?"
       End If
       ' If user clicks the No button, stop QueryUnload.
       If MsgBox(Msg, vbQuestion + vbYesNo, Me.Caption) = vbNo Then Cancel = True
    End Sub参看MSDN帮助:
    ———————————————————————Visual Basic ReferenceQueryUnload Event
          Occurs before a form or application closes. When an MDIForm object closes, the QueryUnload event occurs first for the MDI form and then in all MDI child forms. If no form cancels the QueryUnload event, the Unload event occurs first in all other forms and then in an MDI form. When a child form or a Form object closes, the QueryUnload event in that form occurs before the form's Unload event.SyntaxPrivate Sub Form_QueryUnload(cancel As Integer, unloadmode As Integer)Private Sub MDIForm_QueryUnload(cancel As Integer, unloadmode As Integer)The QueryUnload event syntax has these parts:Part Description 
    cancel An integer. Setting this argument to any value other than 0 stops the QueryUnload event in all loaded forms and stops the form and application from closing. 
    unloadmode A value or constant indicating the cause of the QueryUnload event, as described in Return Values. 
    Return ValuesThe unloadmode argument returns the following values:Constant Value Description 
    vbFormControlMenu 0 The user chose the Close command from the Control menu on the form. 
    vbFormCode 1 The Unload statement is invoked from code. 
    vbAppWindows 2 The current Microsoft Windows operating environment session is ending. 
    vbAppTaskManager 3 The Microsoft Windows Task Manager is closing the application. 
    vbFormMDIForm 4 An MDI child form is closing because the MDI form is closing. 
    vbFormOwner 5 A form is closing because its owner is closing. 
    These constants are listed in the Visual Basic (VB) object library in the Object Browser.ResThis event is typically used to make sure there are no unfinished tasks in the forms included in an application before that application closes. For example, if a user has not yet saved some new data in any form, your application can prompt the user to save the data.When an application closes, you can use either the QueryUnload or Unload event procedure to set the Cancel property to True, stopping the closing process. However, the QueryUnload event occurs in all forms before any are unloaded, and the Unload event occurs as each form is unloaded.
      

  4.   

    //我的程序中用了一个command1的按钮,用于退出程序。在退出程序前时需要做一些我需要的工作例如:Private Sub Form_Unload(Cancel As Integer)
        Command1_Click
    End Sub
      

  5.   

    大哥们,帮我看看,怎么不行呢?Private Sub Form_Unload(Cancel As Integer)
    Dim X As String
    X = MsgBox("你确定要退出系统吗?", vbYesNo, "提示信息")
    Select Case X
    Case 6
    Case 7
    End
    End Select
    End Sub不管是点击“是”或“否”,都退出程序。而这段代码在command1中使用正常。点“是”退出,点“否”不退出。这是为什么呢?
      

  6.   

    这样啊你处理的事件不对,应该处理Form_QueryUnload事件
      

  7.   

    又看了一下,你的程序没有不让程序退出的语句,当然要退出了:
    Private Sub Form_Unload(Cancel As Integer)
    Dim X As String
    X = MsgBox("你确定要退出系统吗?", vbYesNo, "提示信息")
    Select Case X
    Case 6
    End
    Case 7
    Cancel = True
    End Select
    End Sub
      

  8.   

    不过最好还是处理Form_QueryUnload事件,处理这个事件可以知道是什么原因使窗体关闭的
      

  9.   

    处理Form_QueryUnload事件 也不行。
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Dim X As String
    X = MsgBox("你确定要退出系统吗?", vbYesNo, "提示信息")
    Select Case X
    Case 7
    Case 6
    End
    End Select
    End Sub
    还是出现点击“是”或“否”,都退出程序。而这段代码在command1中使用正常。点“是”退出,点“否”不退出。这是为什么呢?高手们帮我想想办法啊?
      

  10.   

    已经OK了,谢谢,随便再问个问题,同样在command1的代码中也没有不让程序退出的语句,为什么可以不退出呢?
      

  11.   

    //同样在command1的代码中也没有不让程序退出的语句,为什么可以不退出呢?你的command1的代码中没有程序退出的语句,当然不会退出了