Results 1 to 7 of 7

Thread: Easy one... Form_Unload

  1. #1

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    Red face

    Ok I've made this work before but can't get it to do it this time. When the user hits X on the form the user is aske if he/she wants to save changes yes and no work fine but the form still unloads if you hit cancel...

    code:

    Private Sub Form_Unload(Cancel As Integer)
    If NoteDirty Then
    Dim z
    z = MsgBox("Would you like to save changes?", vbYesNoCancel, "Save changes?")
    If z = 6 Then
    mnuFileSave_Click
    ElseIf z = 2 Then
    Exit Sub
    Else
    'oh well
    End If
    End If
    End Sub

    Its a replacement for notepad since mine is screwed for some reason....

    Thankz
    Magiaus
    Visual Basic 6.0 SP5
    Visual C++ 6.0 SP5


    The only sovereign you can allow to rule you is reason.

  2. #2
    Addicted Member
    Join Date
    Jan 1999
    Posts
    204
    try
    Code:
    msgbox "need to save it ?", vboksavecancel,"save"
    if vbok = true then
             savefile
    elseif 
               exit 
    else
     
    end if
    off the top of my head , so if it dont work sorry
    WHat would we do with out Microsoft.
    A lot more.

  3. #3
    Hyperactive Member
    Join Date
    Dec 1999
    Posts
    321

    Cancel = 1


    You gotta set the Cancel variable to make sure the program understands you're canceling the unload.


    like this
    Code:
    Private Sub Form_Unload(Cancel As Integer) 
    If NoteDirty Then 
     Dim z 
     z = MsgBox("Would you like to save changes?", vbYesNoCancel, "Save changes?") 
     If z = 6 Then 
      mnuFileSave_Click 
     ElseIf z = 2 Then 
      Exit Sub 
     Else 
     'oh well
     Cancel = 1 ' <--------- 
     End If 
    End If 
    End Sub
    Signed, Rodik ([email protected])
    Programmer,usesVB6ED
    ===========================
    Copyright©RodikCo,2002.

    Dont mind this signature ;] Its old

  4. #4
    Lively Member
    Join Date
    Mar 2000
    Posts
    87

    Thumbs up

    Right Idea, just the wrong event. Use the queryunload event of the form.

    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    
        Dim lResult         As Long
    
        If UnloadMode = vbFormControlMenu Then
            lResult = MsgBox("Would you like to save changes?", vbYesNoCancel, "Save changes?")
            Select Case lResult
                Case vbYes
                    'Do save here
                    
                Case vbNo
                    'Just let it exit anyway
                    
                Case vbCancel
                    'Dont close
                    Cancel = True
                    
            End Select
        End If
        
    End Sub

  5. #5

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    Smile THankz Guys

    But why is it better to use Form_QuerryUnload?
    Magiaus
    Visual Basic 6.0 SP5
    Visual C++ 6.0 SP5


    The only sovereign you can allow to rule you is reason.

  6. #6
    Lively Member
    Join Date
    Mar 2000
    Posts
    87
    With the query unload event you can tell how your program is being unloaded.

  7. #7
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Rotterdam, Netherlands
    Posts
    386
    True, but with the sample code ("If UnloadMode = vbFormControlMenu Then") you only ask to save changes when the user presses the X-button, but when windooz stops (or the program is closed thru the taskmanager) it won't be asked, so in this case it doesn't really matter whether to use the query_unload or the unload event, you always want to save changes.
    Hope this helps

    Crazy D

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width