What Is the Difference Between Form1.Hide and Unload Me?

Hide and Unload Are Techniques in Visual Basic 6

Hide and Unload are techniques in Visual Basic 6—VB.NET does things differently. In VB6, you can see the difference clearly by 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.

Visual Basic 6 Unload Statement

The Unload statement removes the form from memory. In most simple VB6 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.

Visual Basic 6 Hide Statement

To demonstrate Hide, run this code in VB6 so the Hide method of Form1 is executed.

Private Sub Command1_Click()
   Form1.Hide
End Sub

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

Communicating With a Hidden Form

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 VB6 project and then add a Timer component 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 Command button 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 makes Form1 disappear and Form2 appear. However, clicking the button on Form2 uses the Timer component on Form1 to wait 10 seconds before making Form2 disappear and Form1 appear again even though Form1 isn't visible.

Since the project is still running, Form1 keeps appearing every 10 seconds—a technique you might use to drive a coworker batty one day.

Format
mla apa chicago
Your Citation
Mabbutt, Dan. "What Is the Difference Between Form1.Hide and Unload Me?" ThoughtCo, Jan. 29, 2020, thoughtco.com/difference-between-form1hide-and-unload-me-3424279. Mabbutt, Dan. (2020, January 29). What Is the Difference Between Form1.Hide and Unload Me? Retrieved from https://www.thoughtco.com/difference-between-form1hide-and-unload-me-3424279 Mabbutt, Dan. "What Is the Difference Between Form1.Hide and Unload Me?" ThoughtCo. https://www.thoughtco.com/difference-between-form1hide-and-unload-me-3424279 (accessed April 25, 2024).