Close (X) button does not close my application.

My program terminate command is as follows:
  Form1.Hide
  Unload Form1
  End
This works well every time. However, when I use the windows standard (X) button in the upper right hand corner of the window, the application becomes invisible but remains running. The application appears in the task manager and can only be closed using "End Task". The average user will always use the (X) as a way to exit any application. How do I get the (X) button to terminate my program?
Thanks
StephenMShickAsked:
Who is Participating?
 
Farzad AkbarnejadConnect With a Mentor DeveloperCommented:
Hi,
Add End statement to QueryUnload events of forms that you want to click on their (X) button and terminate your program.

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    End
End Sub

-FA
0
 
RejojohnyCommented:
The "End" is not a very good thing to be used as this stops the program abrubtly .. no cleaning of variables .. memory etc happens after the command .. use this code .. remove the ".Hide" .. it is not necessary for u to close the form ...

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   unload form1
   set form1 = nothing
End Sub
0
 
RejojohnyCommented:
ok cool .. u have anway accepted an answer .. but i will still say the "End" is not a nice solution ..understand y ur code was not working ... it was becuase the terminate event was not geting executed .. when u "unload form1" it does not really remove it from the memory .. using the set form1 = nothing would ensure that .. so ur code would still continue to work if u have this statement ..
0
 
StephenMShickAuthor Commented:
Thanks to FarzadA and Rejojohny
I used a combination of both answers as shown below.

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   Unload Form1
   Set Form1 = Nothing
   End
End Sub

Unfortunately I ould only officially accept one answer. I selected the first because it did stop the program. The second answer did the clean up, but left it running. Still need the "End".
0
 
RejojohnyCommented:
the program was still running because some objects created within ur code is still "alive" probably becuase of the form1.hide method .. try removing it .. and without the "end" statement .. I am sure it will work ...

as i said .. the "END" statement has just ended everything at the spot .. that is not really a good solution .. basically u have not exited gracefully ...

have a look at these posting too
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21024448.html
(arthur's comment)

0
All Courses

From novice to tech pro — start learning today.