Library tutorials & articles
Forms
Hiding a form
When a user has finished with a dialog, they will want to close it by clicking a OK button of some sorts. To hide a dialog, you can use the Hide method. The following code hides Dialog1.
Dialog1.Hide
Using the Hide method does what is says. It hides the dialog. The dialog is not actually removed from the memory. When you use the Show method again, any controls will have kept their values. This is useful if the dialog will be shown and hidden a lot. However, forms such as the Splash form that will only be shown once should be removed from the memory. To do this, use the Unload method. If you do not do this, you will end up with lots of forms in the memory, not being used. If you have many forms with lots of controls saved in the memory, you may find you application eats up all the avaiable memory, and the whole system crashed. If you need to keep some values, save them to a variable, which can be accessed again when you need to load the form again. The following code removes Dialog1 from the memory.
Unload Dialog1
A very common mistake is to use the End statement. This is not good practice, as it does not always free up any of the memory that was allocated for your application.
When you Unload a form, its QueryUnload and Unload events occur. In both events, you can cancel the form unloading by setting Cancel = -1. In the QueryUnload event, you can also find out how the form is being unloaded by checking the UnloadMode value. This can be one of the following:
| Constant | Description |
| vbAppTaskManager (3) | Windows Task Manager is closing the application. |
| vbAppWindows (2) | Current Windows session ending. |
| vbFormCode (1) | Unload method invoked from code. |
| vbFormControlMenu (0) | User has chosen Close command from Control menu box on form, or clicked the X button. |
| vbFormMDIForm (4) | MDI child form is closing because the MDI form is closing. |
| vbFormOwner (5) | The owner of the form is closing. |
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
In my program I have two forms. The user is asked to input a number in my custom-made form (input box) which has "OK", "CANCEL" and the input textbox.
Basically the problem is if if the user presses "Cancel" in my custom input box, the sub in the first form will just continue being played out when I want to pretty much "cancel" it. How can I make the "cancel" button in my second form make the sub in the first form end?
Thanks!
true, i noticed that my tray icon doesnt disappear automatically, cause the unload method of the form its on isnt loaded...
ah well it works fine if you need to really kill it i guess
You don't really want to use End, as it forcefully terminates everything, rather than gracefully telling the forms that they're about to be closed (ie the Unload event), and then removing them from memory...
i know an even simpler method:
Public Sub CloseAll()
End
End Sub
it works fine for me
If you replace "vbModeless" with "vbModal" then no window below can be clicked on! Great for setting options.
okay, we'll welcome to the both of you.
To show a form, you can do it in a few ways.
If you want to show aform and stop the process from being able to execute any more comments until the form is close we would do this:
frmForm.Show vbmodal
what that means is say this scenario:
FormLoad()
on error goto ErrH
dim i as integer
i = 1000
i = i / 0
errH:
frmForm.Show vbmodal
Resume Next
end sub
now that will cause a 'Divide by Zero' error when you run it because of the i=i/0 line. We are using Eroror handling(first line under the FormLoad() statement) so it will goto the ErrH: area and execute what ever is underneath it. By running it in a Modal state we will stop the excution of commands until the form is dismissed by the user.
Now what if you didnt want to show the form and stop executing? Simple use the .Show method on the form.
Okay i hear you say "Okay smarty pants.. what if i want to float the form ontop of a form?(Eg. Form1 ontop of Form2?) huh? Huh? think you so smart try that one? come on comone? huh... wanna start me now? huh?"...
we'll no need to get so jumpy... we simply do this: form1.show , form2 where Form2 is the parent form of form1!
now when your unloading you forms its important to unallocate the memory that the form may be occupying. You can achieve this by this:
{rivate sub Form_Unload(cancel as integer)
On error resume next '// Just incase
set <form name> = nothing '// unallocatoin
End sub
As for loading and unloading as HyperHacker has said... you would use that if your intending to load a form prior to showing it, typical scenario is when you display a Splash screen, in the background all the forms are being loaded(yes theres a reason for the Splash screen and its not just for nice logos!)...
Does this help?
Say your button is command1, then, if you mean what I think you mean:
Private Sub command1.Click()
form1.hide
form2.show
end sub
Simple enough to understand. You can use .load and .unload rather than .show and .hide to make the program use less memory, but it'll be slower (and I'm having trouble getting .unload to work).
[edit] Aha, here it is. To use the more RAM-friendly but slower load/unload, you would do this:
unload form1
load form2
Strange how form1.load didn't cause an error, but .unload did. O_o
>>Button to Link to another Form<<
Please Help!
I cannnot get the inner form width on a Mdi form expanded to more than normal screen resolution.
Check out the following code:
Dim frmForm As Form
On Error Resume Next
'close all forms
For Each frmForm In Forms
Unload frmForm
Next
End
End Sub
You use the
http://www.developerfusion.com/show/72/5/
You mensioned that loaded forms take memory, but never mension how to close them all when your program ends. Probably the FIRST thing you need to learn if your application uses more then one form. Visual Basic does Not nessasarily release this memory otherwise, I know
This thread is for discussions of Forms.