Discussion Locked

Program won't exit when closed

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I created a program in Visual Basic using Visual Studio, with multiple forms. The first form calls the individual other forms by "formx.show". When finished with the forms, I use "formx.hide". This way the variables used in each form are available to each others.

The code to exit consists of:

Public Sub UnloadAllForms()
Dim Form As Form
For Each Form In Forms
Unload Form
Set Form = Nothing
Next Form
End Sub

then to call an exit is use:
Call UnloadAllForms
End

However, when I create an exe from this program, whenever I load and unload a form inside the program, exit only makes the program disappear. I must exit the program using the Task Manager.

How do I get it to exit by itself?

7 replies

by zeshanzia

The form from which you are calling others is your parent form. You must Exit or terminate the application with this main form.

Use 'End' keyword in your Main Parent Form and provide some mechanism like button for that purpose

by tclere

Observation: You should not use the object name "Form" since it is a reserved keyword. Instead, try something like:

Dim myForm As Form
For Each myForm in Forms...

by madestroitsolutions

I tested your code and It works fine (assuming these are standard forms, not MDI), which means the problem is not here. You must be doing something else in some other place that is causing the program to crash and stay as a process.
Do this:

- Recompile
- Check the rest of your code

if that doesnt work, then reinstall VB.

By the way, consider the tip on answer #2, dont use reserved keywords, and try changing your approach. Why not use an MDI form? MDI forms will close their child windows automatically. You can preserve the values entered in your forms in other ways, like the registry, or global variables in a module, or a database perhaps. It will make your life easier. BELIEVE ME, been there... lol, good luck!

by dartfrogs404

This problem occurs on two seperate computers. I tried uninstalling and reinstalling Visual Studio, still no luck. I'm not farmilliar with MDI forms, although i will try that. Also, i'll preserve my data using global variables, because apparentlyhiding and showing forms isn't the best way to do it, that i didn't know. Thanks for your help.

This discussion is closed to new replies.