Click to See Complete Forum and Search --> : VB question
LabRat
June 20th, 2001, 11:27 AM
I'm taking a VB course and just got into multiple forms. DUring the course of my project, I had to close the program from the current form.
My question is this: " Is there an action associated with the 'X' box in the Window controls where I can issue unload commands for all forms resident in memory?"
I posed this same question to my instructor and he just scratched his head and grinned. Then stated that there was no such action without creating an exit button with the unload commands in it. Doesn't make sense since there is the Form_load event it would stand to reason that a Form_Unload action or something similar would be available.
Thanks for any help.
Deity
June 20th, 2001, 11:47 AM
There is exactly a Sub Form_Unload. This is an event that runs when the form closes. You could write a simple routine that whenever any form unloads, it unloads all other forms.
Not sure if this is what you were looking for, but let me know if I can help more.
LabRat
June 20th, 2001, 12:11 PM
Originally posted by Deity:
<STRONG>There is exactly a Sub Form_Unload. This is an event that runs when the form closes. You could write a simple routine that whenever any form unloads, it unloads all other forms.
Not sure if this is what you were looking for, but let me know if I can help more.</STRONG>
That is <STRONG>exactly</STRONG> what I was looking for. Could you tell me how to invoke it? I tried tools --> Add Procedure Sub --> and named the procedure as Form_Unload When that didn't work for me, I also tried naming it to Form_Close
Is there a certain level of VB that I need for some of these functions (professional perhaps)?
Thanks for verifying that I'm not crazy <IMG SRC="smilies/tongue.gif" border="0">
Deity
June 20th, 2001, 01:30 PM
The Form_Unload is a built-in event of the object. I'm assuming you're using MS VB6, and since I don't know the technical names of the location, I'll just describe how you change it. Above your code you have two drop down boxes, the left one lists the objects of the form. One of those objects is the form itself. The right drop down box lists all the events associated with that object. In that drop down list, select Unload. This will automatically create the procedure in your code and you should be able to place whatever code you wish there. I used a similar routine once, in a multiple form project. <IMG SRC="smilies/smile.gif" border="0">
LabRat
June 20th, 2001, 01:49 PM
<STRONG>THANKS!!</STRONG>
Deity
June 20th, 2001, 02:14 PM
Let me know if you are able to get the code to work properly. I'm trying to stay up with my VB code.
LabRat
June 20th, 2001, 02:24 PM
Should be able to tell you tonight when I get home, but I remember the two bars that you referenced and am sure it will work properly once the right channels are created to create skeleton of the procedure.
Thanks again. I'll definitely let you know how it turns out.
opiate
June 20th, 2001, 03:22 PM
Originally posted by Shift_2_Break:
<STRONG>I'm taking a VB course and just got into multiple forms. DUring the course of my project, I had to close the program from the current form.
My question is this: " Is there an action associated with the 'X' box in the Window controls where I can issue unload commands for all forms resident in memory?"
I posed this same question to my instructor and he just scratched his head and grinned. Then stated that there was no such action without creating an exit button with the unload commands in it. Doesn't make sense since there is the Form_load event it would stand to reason that a Form_Unload action or something similar would be available.
Thanks for any help.</STRONG>
use the End function.
Terminates execution immediately. Never required by itself but may be placed anywhere in a procedure to end code execution, close files opened with the Open statement and to clear variables.
LabRat
June 20th, 2001, 05:09 PM
Thanks for the comment Opiate, but I'd need to enter end in the _unload event in order to successfully complete my project. There is only supposed to be one form with an exit command to terminate the project and I don't believe in leaving items resident in memory because an idiot programmer (me) did not plan for the contingency of someone terminating a form without using a menu or command option given on the form.
Another <STRONG>workaround</STRONG> is to disable the 'x' button on the window frame for all forms and force the user to use only those controls available on the form itself.
Sorry for the confusion and hope that clarifies.
LabRat
June 20th, 2001, 08:10 PM
Thanks Diety and Opiate. The drop menus set up the procedure and entering end only does the trick for all the forms. I'm (almost) more satisfied that M$ is not ALL bad <IMG SRC="smilies/wink.gif" border="0">
Thanks again.
Deity
June 20th, 2001, 09:12 PM
Good to hear it! Glad I could help. <IMG SRC="smilies/smile.gif" border="0">
opiate
June 21st, 2001, 12:17 AM
Sample
Create a project exe...
1. create 3 forms:
form1
form2
form3
2. on the first form create a command button:
cmdExit
3. on the first form create menus:
Caption = Menu, Name = mnuMenu
Child Caption = Exit, Name = mnuExit
Caption = Forms, Name = mnuFrm
Child1 Caption = Form2, Name = mnuForm1
Child2 Caption = Form3, Name = mnuForm2
4. Create a Module
here is the code...
5. Form1:
Private Sub cmdExit_Click()
Call UnloadAll
End Sub
Private Sub mnuExit_Click()
Call UnloadAll 'Calls the Public Sub in the Module
End Sub
Private Sub mnuForm2_Click()
Form2.Show
End Sub
Private Sub mnuForm3_Click()
Form3.Show
End Sub
6. Module:
Public Sub UnloadAll() 'Call this public sub on the main form mnuExit and cmdExit.
On Error Resume Next 'An error will occur if you try and close forms that are not open.
' On Error Resume ignores the errors and continues processing code.
Unload Form1
Unload Form2
Unload Form3
End Sub
Hope that helps. This will unload all the forms from the memory.
cheers,
opiate
Deity
June 21st, 2001, 10:39 AM
Opiate -
Here's a scenario for you...in a situation similar to this, using multiple forms, but the number of open forms can vary...would there be a way to close all open forms EXCEPT for the main form. Just curious if there was a way, without having to list each form, like in your example:
Unload Form1
Unload Form2
Unload Form3
Any thoughts?
opiate
June 21st, 2001, 12:54 PM
Originally posted by Deity:
<STRONG>Opiate -
Here's a scenario for you...in a situation similar to this, using multiple forms, but the number of open forms can vary...would there be a way to close all open forms EXCEPT for the main form. Just curious if there was a way, without having to list each form, like in your example:
Unload Form1
Unload Form2
Unload Form3
Any thoughts?</STRONG>
Try using this in the module and get rid of the list:
Public Sub UnloadAll()
On Error Resume Next
Uload All 'this will close all open forms
form1.show 'this will open your main form
End Sub
Deity
June 21st, 2001, 01:26 PM
That makes sense. Thanks for the info.
LabRat
June 21st, 2001, 01:46 PM
From my limited understanding frm*.Show will also call the unloaded form to memory
By using the module UnloadAll procedure and recalling frmMain to memory, you would lose any unsaved data settings. Is this correct and, if so, is there another method besides assigning global variables to the form's labels and text boxes to reinstate the original data on the freshly loaded instance of themain form?
(Hope that came out right.. <IMG SRC="smilies/wink.gif" border="0"> )
opiate
June 21st, 2001, 09:11 PM
Originally posted by Shift_2_Break:
<STRONG>
(Hope that came out right.. <IMG SRC="smilies/wink.gif" border="0"> )</STRONG>
I understand...
You are right! I was not aware of the full project that you all were trying to create. I was just giving differnt examples. The only way I can think to keep settings resident throught the program would be to save and get registry values on load and unload.
opiate
Good Luck..
antonye
June 22nd, 2001, 06:14 AM
Just as a tip, you can always do this:
dim frmX as Form
for each frmX in Forms
Unload frmX
next
This will cause each of your forms to be unloaded from memory - the list of forms in your program is held in the Forms collection.
Be careful though as it will unload the form you call this from too!
You also need to understand the differences between Show,Hide and Load,Unload. Show and Hide are used to display the forms without removing them from memory and thus losing any associated data. Using the .Show method will cause the form to be loaded (and call the _Load event) if the form is not currently in memory.
In the same way, the Hide method with simply remove the form from the screen without destroying it's associated data.
Just for another tip, always use the _QueryUnload sub rather than the _Unload as QueryUnload is always called, whereas there are some situations where the _Unload sub does not get called.
Deity
June 22nd, 2001, 10:33 AM
Thanks antonye for clearing that up. <IMG SRC="smilies/smile.gif" border="0"> I'm in no way a VB guru, but I help where I can. And you gave the exact example I was thinking of *but couldn't remember just right* with the loading and unloading of multiple forms.