Link to home
Start Free TrialLog in
Avatar of TheRedGuy
TheRedGuy

asked on

App won't Close in Development environment

I have an application that (sometimes) will not close down via the Control Box in the top-right corner. All .exes compiled from the same source function fine.

Any ideas what is wrong....

Post comments if you need more info. I have no idea what may be of relevance to help you answer this question. I will say, though, that all forms are set to Nothing....
Avatar of Erick37
Erick37
Flag of United States of America image

Make sure all forms are unloaded and that you do not reference any form after it is unloaded.  Also avoid the use of the END statement, that will terminate the program without unloading.  Sample unloading code:

'~~~~~~~~~Main Form Code~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub Form_Unload(Cancel As Integer)
    Call UnloadAllForms(Me.Name)
End Sub
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


'~~~~~~~~~~~~~~~~~~~~Module Code~~~~~~~~~~~~~~~~~
Public Sub UnloadAllForms(sFormName As String)
    Dim Form As Form
   
    For Each Form In Forms
        If Form.Name <> sFormName Then
            Unload Form
            Set Form = Nothing
        End If
    Next Form
End Sub
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Avatar of nini
nini

Do you mean by "will not close down" that the X clicks and pushes down but then  there is no termination of the program?
Avatar of TheRedGuy

ASKER

Correct. The 'X' pushes down but does nothing else. I have placed breakpoints in the QueryUnload event and the Unload event but neither piece of code is called...
Try placing a message box in the form events
Paint
Deactivate
QueryUnload
Unload
and Terminate
to find where the form may be reloading and check your code for implicit references to the form or another form not loaded but which may have a reference to the the form your trying to close in the load event. The Paint and Deactivate events may behave differently outside the IDE
ASKER CERTIFIED SOLUTION
Avatar of nini
nini

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

<SNIP> ....

Correct. The 'X' pushes down but does nothing else. I have placed breakpoints in the QueryUnload event and the Unload event but neither piece of code is called...

..... <SNIP>

Please note:  everywhere I mention the QueryUnload event handler below, the same applies for the Unload event handler.


Place a break point in all of your Query_Unload event handlers for every form.  ALL forms' Query_Unload handler will be called UNLESS one of them has returned CANCEL.  This has to be what is happening if the Query_Unload where you had placed a breakpoint was not called - because VB has to call AT LEAST one of them.

To emphasize what had been said above ANY reference to any form or a control on a form will cause that form to be loaded by VB even if you have never shown the form.

As annoying as this may be, you can place a message box call in each Form_Load handler and each Form_Terminate handler that says something like Form Load [formname] and Form Terminate [formname] respectively.

The Terminate event is only received after Query_Unload and Unload events returned with Cancel = False!


If you should see a message box pop up that says load after you had seen a message box for the same form that said Terminate than you have identified which form has been reloaded and you can go through your Query_Unload handlers until you find the one that has a reference to that form.




After re-reading your question I would also like to add something else

From your question ....

I have an application that (sometimes) will not close down via the Control Box in the top-right corner. All .exes compiled from the same source function fine.

.....

The key word I see in this is "sometimes".  Make sure that you haven't tried to build any logic that assumes the order in which each form will be unloaded.  The only way to control the order in which forms are unloaded is to explicitly call the Unload [formname] statement for each form.





OK, everyone, thanks for all the comments. I don't think anything has been said, however, that I haven't thought of or that isn't already incorporated into the app. It used to work fine and I've made no significant changes so I can't understand why it's now misbehaving!

What I really don't get is why the QueryUnload event isn't called when I click the 'X'. I would have thought that would be the first bit of code that would run in response to the click.

Or am I wrong (or not getting something
that one of you has said?
OK, everyone, thanks for all the comments. I don't think anything has been said, however, that I haven't thought of or that isn't already incorporated into the app. It used to work fine and I've made no significant changes so I can't understand why it's now misbehaving!

What I really don't get is why the QueryUnload event isn't called when I click the 'X'. I would have thought that would be the first bit of code that would run in response to the click.

Or am I wrong (or not getting something
that one of you has said?
OK, everyone, thanks for all the comments. I don't think anything has been said, however, that I haven't thought of or that isn't already incorporated into the app. It used to work fine and I've made no significant changes so I can't understand why it's now misbehaving!

What I really don't get is why the QueryUnload event isn't called when I click the 'X'. I would have thought that would be the first bit of code that would run in response to the click.

Or am I wrong (or not getting something
that one of you has said?
Can you close the program from the stytem menu's Close item?
perhaps you try to Click the X while some loop is proccesing itself...
that's why it is SOMETIMES.

try to add a DoEvents Statment in all your loops(if exists) and check it again.

ATM
Erick37 - Yes, no problem there.

AnswerTheMan, Nothing doing (literally!)

All,

Incidentally, in the form's Load event, I set it's Height and Width properties and Maximize it. Reason being that I want the Restore button to set the form to a size such that some of the controls are still visible; in Maximised state all supported functionality is available but app can be 'Restored' to a size where partial functionality is available.

Interestingly, if I change any of the properties that relate to size and position prior to running the app I don't get the problem. I'm confused!

TheRedGuy
i guess it's time for some code sample.
since this is weired behaviour, it is almost sure you have done something wrong in your code.
Is this the Only Form in the project ?
are you sure there is no situation that as a result of this resizing there are 2 forms exactly covering each other....?
is there any MDI form there ?

a lot of questions. few details.
OK folks, found the problem. I recently changed the form's .BorderStyle property to 'none'. According to the Help file, you can't have set the .ControlBox property set to 'true'.

Sorry if I wasted anybody's time. It didn't occur to me that this may be the case (and, frankly, I can't see why it has to be).

I guess Nino's suggestion to check the form's properties was the one that led to the solution.

I'll post points if he posts as answer.

Thanks to all

That's odd.  In VB5, if you have a borderless form, there is no caption bar or system menu or min/max/close buttons.
That's what I thought the Help File was suggesting but, sure enough, the app would fire and up and run, showing no border and the min/max/close buttons.....