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.

Comments

  1. 02 Jul 2005 at 17:09

    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!

  2. 17 Feb 2005 at 14:37

    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

  3. 17 Feb 2005 at 13:38

    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...

  4. 17 Feb 2005 at 13:19

    i know an even simpler method:


    Code:

    Public Sub CloseAll()
       End
    End Sub


    it works fine for me

  5. 31 May 2003 at 02:11

    If you replace "vbModeless" with "vbModal" then no window below can be clicked on!  Great for setting options.

  6. 27 Mar 2003 at 19:22

    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:

    Code:

    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 Form
    Load() 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:


    Code:

    {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?

  7. 27 Mar 2003 at 17:43

    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

  8. 24 Feb 2003 at 03:41
    Im very new at VB, and I just need to know how to make a

    >>Button to Link to another Form<<

    Please Help!
  9. 09 Aug 2002 at 13:26

    I cannnot get the inner form width on a Mdi form expanded to more than normal screen resolution.

  10. 08 Mar 2002 at 15:21

    Check out the following code:

    Code:
    Public Sub CloseAll()


    Dim frmForm As Form
    On Error Resume Next
    'close all forms
    For Each frmForm In Forms
       Unload frmForm
    Next
    End


    End Sub

  11. 08 Mar 2002 at 14:55

    You use the

    Code:
    Unload theForm
    statement, as mentioned on


    http://www.developerfusion.com/show/72/5/

  12. 08 Mar 2002 at 14:16

    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

  13. 01 Jan 1999 at 00:00

    This thread is for discussions of Forms.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related discussion

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...

We'd love to hear what you think! Submit ideas or give us feedback