Difference form.Close() and form.hide()
Categories:
VB.NET: Understanding the Difference Between Form.Close() and Form.Hide()

Explore the fundamental differences between Form.Close()
and Form.Hide()
in VB.NET, including their impact on application lifecycle, resource management, and user experience.
In VB.NET Windows Forms applications, managing the visibility and lifecycle of forms is a common task. Two methods often used for this purpose are Form.Close()
and Form.Hide()
. While both can make a form disappear from the user's view, they operate very differently under the hood, impacting application performance, resource usage, and overall behavior. Understanding these distinctions is crucial for writing robust and efficient applications.
Form.Hide(): Temporarily Concealing a Form
The Form.Hide()
method simply makes a form invisible to the user. The form itself, along with all its controls and associated resources, remains loaded in memory. This means that the form's state is preserved, and it can be quickly shown again without needing to be re-initialized. Form.Hide()
is ideal for scenarios where you want to temporarily remove a form from view, perhaps to switch to another form or to keep it available for later use without incurring the overhead of recreation.
Private Sub btnHide_Click(sender As Object, e As EventArgs)
Me.Hide()
End Sub
Private Sub btnShow_Click(sender As Object, e As EventArgs)
Me.Show()
End Sub
Example of hiding and showing a form in VB.NET
Form.Hide()
when you intend to reuse the form instance multiple times during the application's lifetime, such as a settings dialog or a secondary data entry form that might be opened and closed frequently.Form.Close(): Terminating a Form's Existence
In contrast, Form.Close()
initiates the process of closing a form. When Form.Close()
is called, the form attempts to close, triggering a series of events (like FormClosing
and FormClosed
). If the closing process is not canceled (e.g., by setting e.Cancel = True
in the FormClosing
event), the form is disposed of. This means all its resources are released, and the form object itself is marked for garbage collection. Once a form is closed, it cannot be shown again; you would need to create a new instance of the form.
Private Sub btnClose_Click(sender As Object, e As EventArgs)
Me.Close()
End Sub
Private Sub MyForm_FormClosing(sender As Object, e As FormClosingEventArgs)
If MessageBox.Show("Are you sure you want to close?", "Confirm Close", MessageBoxButtons.YesNo) = DialogResult.No Then
e.Cancel = True ' Cancel the close operation
End If
End Sub
Example of closing a form and handling the FormClosing event
Form.Close()
on the application's main form will typically terminate the entire application, unless Application.Exit()
is explicitly called or Application.ExitThread()
is used in a multi-threaded scenario.Key Differences and When to Use Each
The choice between Form.Hide()
and Form.Close()
boils down to whether you need to preserve the form's state and resources for potential reuse, or if you want to permanently remove it from memory. Hide()
is for temporary invisibility, while Close()
is for permanent termination and resource release. Misusing these methods can lead to memory leaks (if forms are hidden but never closed when no longer needed) or unnecessary performance overhead (if forms are constantly closed and recreated when they could have been hidden).
flowchart TD A[User Action: Button Click] --> B{Method Called?} B -->|Form.Hide()| C[Form becomes Invisible] C --> D{Form in Memory?} D -->|Yes| E[State Preserved] E --> F[Resources Held] F --> G[Can be Shown Again] B -->|Form.Close()| H[FormClosing Event Triggered] H --> I{Closing Canceled?} I -->|No| J[FormClosed Event Triggered] J --> K[Form Disposed] K --> L[Resources Released] L --> M[Marked for GC] M --> N[Cannot be Shown Again] I -->|Yes| C
Flowchart illustrating the lifecycle differences between Form.Hide() and Form.Close()
Consider the following scenarios to guide your decision:
1. For Reusable Dialogs or Secondary Windows
If you have a form that acts as a settings dialog, a search window, or any other secondary interface that the user might open and close multiple times, use Form.Hide()
when the user dismisses it. This keeps the form's state intact and allows for quicker re-display.
2. For Main Application Windows or One-Time Use Forms
When a form represents a distinct stage in a workflow that won't be revisited, or if it's the main application window, use Form.Close()
. This ensures that resources are properly released and the application's memory footprint is managed efficiently.
3. Managing Application Exit
If you want to ensure the entire application exits when a specific form is closed, make sure that form is set as the startup form, or explicitly call Application.Exit()
after closing the last relevant form.