School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,161 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,026 people online right now. Registration is fast and FREE... Join Now!



Closing the program

Closing the program Rate Topic: -----

#1 centers  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 2
  • Joined: 25-September 07


Dream Kudos: 0

Post icon  Posted 25 September 2007 - 12:14 PM

Well I started writing code for a control simulator and before I knew it I had 40 Forms and 2 Modules. It runs well when I run it in the IDE but when I made it into an exe it ran ok but when I clicked on the close button (the X in the upper right hand corner) not all of the forms closed.
So what is the best way to Close or Exit a program?

This post has been edited by centers: 25 September 2007 - 01:19 PM

Was This Post Helpful? 0
  • +
  • -


#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 7,676
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Posted 25 September 2007 - 03:55 PM

What you will want to do is loop through all the forms and unload them. VB will fully close as soon as you close all forms. This is even recommended over the use of End and can be done with a snippet like the following...

Public Sub UnloadAllForms()

	  Dim objForm As Form
		 
	  ' Loop through all the forms and unload each
	  For Each objForm In Forms
			Unload objForm
	  Next

	  Set objForm = Nothing
End Sub 



Then all you have to do is call the function to unload all the forms loaded. I hope this is a solution that will work for you. Good luck.

:)
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 4,566
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

Posted 26 September 2007 - 06:22 AM

Quote

This is even recommended over the use of End

Exactly.

The End statement stops code execution abruptly without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code.

Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed. Object references held by other programs are invalidated.

The End statement provides a way to force your program to halt. For normal termination of a Visual Basic program, you should unload all forms. Your program closes as soon as there are no other programs holding references to objects created from your public class modules and no code executing.

Source : MSDN
Was This Post Helpful? 0
  • +
  • -

#4 centers  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 2
  • Joined: 25-September 07


Dream Kudos: 0

Posted 26 September 2007 - 06:39 PM

Thanks for the snippet.
How would I trigger this?
I'm thinking maybe put a cmdClose() in one of the modules. Would that be triggered from any of the forms if the Close ( X in the top right had corner of a form) was Clicked?


View PostMartyr2, on 25 Sep, 2007 - 04:55 PM, said:

What you will want to do is loop through all the forms and unload them. VB will fully close as soon as you close all forms. This is even recommended over the use of End and can be done with a snippet like the following...

Public Sub UnloadAllForms()

	  Dim objForm As Form
		 
	  ' Loop through all the forms and unload each
	  For Each objForm In Forms
			Unload objForm
	  Next

	  Set objForm = Nothing
End Sub 



Then all you have to do is call the function to unload all the forms loaded. I hope this is a solution that will work for you. Good luck.

:)

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month