Visual Basic – What is the difference between Form1.Hide and Unload Me?

--> (Word) --> (PDF) --> (Epub)
This article has been published [fromdate]

First, keep in mind that both of these are techniques in VB 6. VB.NET does things differently. In VB 6 you can see the difference very clearly by simply creating a form with a CommandButton component and a test statement in the Click event. Note that these two statements are mutually exclusive, so only one can be tested at a time.

The Unload statement removes the form from [gs memory]. In most simple VB 6 projects, Form1 is the startup object so the program stops running too. To prove this, code the first program with Unload.

Private Sub Command1_Click()
  
Unload Me
End Sub

When the button is clicked in this project, the program stops.

To compare with Hide, run this code in VB 6 so the Hide method of Form1 is executed.

Private Sub Command1_Click()
  
Form1.Hide
End Sub

Notice that Form1 diappears from the screen, but the square "End" icon on the Debug toolbar shows the the project is still active. (If you're still in doubt, the Windows Task Manager that is displayed with Ctrl-Alt-Del shows the Project is still in Run mode.)

The Hide method only removes the form from the screen. Nothing else changes. For example, another process can still communicate with objects on the form after the Hide method is called. Here's a program that demonstrates that. Add another form to the VB 6 project and then add a Timer component and and this code to Form1:

Private Sub Command1_Click()
  
Form1.Hide
Form2.Show
End Sub
Private Sub Timer1_Timer()
  
Form2.Hide
Form1.Show
End Sub

In Form2, add a Commandbutton control and this code:

Private Sub Command1_Click()
  
Form1.Timer1.Interval = 10000 ' 10 seconds
Form1.Timer1.Enabled = True
End Sub

When you run the project, clicking the button on Form1 will make Form1 disappear and Form2 appear. But clicking the button on Form2 will use the Timer component on Form1 to wait ten seconds before making Form2 disappear and Form1 appear again even though Form1 isn't visible.

Since the project is still running, Form1 will keep appearing every ten seconds - a technique you might use to drive a co-worker batty sometime.

SOURCE

LINK (Visualbasic.about.com)

LANGUAGE
ENGLISH