Unload Event

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Unload Event

Post by D Willett »

On closing a form ( MDi frmMain & frmPaint ) I have a routine which checks if an image has been changed.
If true then the image is saved and Form1024Image shows, loads.

Code: Select all

    If Dirty = True Then
        x = MsgBox("The image has been changed." & vbCrLf & vbCrLf & "Do you wish to save the changes?", vbExclamation + vbYesNo, "The image has been changed")
If x = 6
If the user loads an image to edit and then changes his mind and just closes the form ( Top right X ) then the whole program closes and stays in memory. The user has to then Ctl-Alt-Del to end the program before it can be used again. I'm strugging to capture this event but below is the code which I think is causing the issue.
If the user has changed his mind then he should close the form and return to Form1024Image.

Code: Select all

                SavePicture frmMain.ActiveForm.Buffer.Image, DP & FF
                ImgConvert DP & FF, DP & FF, True
                frmMain.ActiveForm.SetDirtyFalse
            
            End If

                Set fso = Nothing
                Form1024Image.Show
                SetDirtyFalse
            Else

                If x <> 6 Then Cancel = True
                End If
                'Unload Me
                Set fso = Nothing
                Form1024Image.Show
                SetDirtyFalse
                Unload Me
            End If
        
Can anyone see the fault in my unload event?
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 79426
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Unload Event

Post by HansV »

You didn't post the entire procedure, but you seem to cancel the unload event and then to unload the form - that's confusing.

You can prevent the form from being closed by clicking the "x" in the upper right corner by using the QueryUnload event of the form - it passes an argument that tells you how the form is being closed. See DeActivate, Unload, QueryUnload, and Terminate Events and the official reference QueryUnload event.
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Unload Event

Post by D Willett »

Hi Hans.
Here's the full unload event which is made up from a couple of routines I put together.
Perhaps there's something in there untowards which needs attention.
There's a good article I've just been looking at which may be useful to other users:

http://www.pcnetcom.net/visual_b.htm

So my queryUnload should look like ?

Code: Select all

                Set fso = Nothing
                Form1024Image.Show
                SetDirtyFalse
                Unload Me
Full Unload:

Code: Select all

Private Sub Form_Unload(Cancel As Integer)
    Dim JN As String                                       'Job Number
    Dim fn As String                                       'FileName
    Dim AN As String                                       'Archive Name
    Dim DP As String                                       'Default Path
    Dim AP As String                                       'Archive Path
    Dim intMax As Integer                                  'Maximum File No
    Dim intPos As Integer
    Dim pos1 As Integer, pos2 As Integer 'Position ??
    Dim intPos2 As Integer
    Dim intSeq As Integer                                  'Sequence
    Dim FF As String                                       'Final File
    Dim strFolderName As String
    Dim fso As FileSystemObject
    Dim fName As String
    Dim FileName As String
    Dim IP As ImageProcess
    Dim img As ImageFile
    Dim x As Integer
    Dim txtJob As String
    
    If Dirty = True Then
        x = MsgBox("The image has been changed." & vbCrLf & vbCrLf & "Do you wish to save the changes?", vbExclamation + vbYesNo, "The image has been changed")
        
    If x = 6 Then

    'Check if list contains any selections
    On Error Resume Next
    txtJob = frmMain.txtJob.Text
    ' Get position of -
    pos1 = InStrRev(txtJob, "-")
    ' Get position of \
    pos2 = InStrRev(txtJob, "\", pos1 - 1)
    ' Extract part between \ and -
    fName = Mid(txtJob, pos2 + 1, pos1 - pos2 - 1)
        
    JN = fName
    
    'Ensure Job No is entered

    If IsNumeric(JN) Then 'Check If Estimate Number or Registration

        intMax = 0
        DP = "L:\MMPDF\ConsoleFiles\" & JN & "\"
        AP = "L:\MMPDF\Archive\" & JN & "\"
        fn = Dir(DP & JN & "-*.jpg")
        strFolderName = "L:\MMPDF\ConsoleFiles\" & JN & "\"

        If Dir(strFolderName, vbDirectory) = "" Then
            MkDir strFolderName
        End If

        Do Until fn = ""
            intPos = InStrRev(fn, ".")
            intPos2 = InStrRev(fn, "-", intPos - 1)
            intSeq = Val(Mid(fn, intPos2 + 1, intPos - intPos2 - 1))
            If intSeq > intMax Then
                intMax = intSeq
            End If
            fn = Dir
        Loop

        AN = Dir(AP & JN & "-*.jpg")
        Do Until AN = ""
            intPos = InStrRev(AN, ".")
            intPos2 = InStrRev(AN, "-", intPos - 1)
            intSeq = Val(Mid(AN, intPos2 + 1, intPos - intPos2 - 1))
                If intSeq > intMax Then
                    intMax = intSeq
                End If
            
            AN = Dir
        Loop
                        Set img = CreateObject("WIA.ImageFile")
                        Set IP = CreateObject("WIA.ImageProcess")
                        
                        
                intMax = intMax + 1

                If intMax = 999 Then MsgBox "You Have Exceeded The Amount Of Images" & vbCrLf & _
                "That Can Be Saved For This File" & vbCrLf & _
                "You Must Report This To Dave Willett", vbCritical, "Error": Exit Sub
                
                FF = JN & "-" & Format(intMax, "000") & ".jpg"
                FileName = frmMain.ActiveForm.Buffer.Image
                SavePicture frmMain.ActiveForm.Buffer.Image, DP & FF
                ImgConvert DP & FF, DP & FF, True
                frmMain.ActiveForm.SetDirtyFalse
            
            End If

                Set fso = Nothing
                Form1024Image.Show
                SetDirtyFalse
            Else
                'FileName = GetSaveName("Save As...")
                 
                'If FileName <> "" Then
                '    SavePicture frmMain.ActiveForm.Buffer.Image, FileName
                '    frmMain.ActiveForm.Buffer.Tag = FileName
                '    frmMain.ActiveForm.Caption = FileName & " - " & frmMain.ActiveForm.GetZoomFactor & "%"
                '    SetDirtyFalse
                'Else
                '    x = MsgBox("Save failed." & vbCrLf & vbCrLf & "Close anyway?", vbCritical + vbYesNo, "Save failed.")
                If x <> 6 Then Cancel = True
                End If
                'Unload Me
                Set fso = Nothing
                Form1024Image.Show
                SetDirtyFalse
                Unload Me
            End If
      
End Sub
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 79426
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Unload Event

Post by HansV »

You should not have a line

Unload Me

in the Form_Unload event procedure (nor in the Form_QueryUnload procedure). When the event occurs, something else has already initiated the unload. You can cancel the unload if desired by using

Cancel = True

If you do not use Cancel = True, the unload will proceed, so you shouldn't use Unload Me again.
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Unload Event

Post by D Willett »

Got it .. Tested using QueryUnload and it works great.

Regards
Cheers ...

Dave.