Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
VB.Net: Hide a form on closing.
From Wiki
If you don't really want the user to close a form but keep the form in memory for faster loading then you can do it like this
in .Net 1.0 and 1.1 you would do this
- Public Class Form1
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Me.Close()
- End Sub
- Private Sub Form1_IsClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.IsClosing
- e.Cancel = True
- Me.Hide()
- End Sub
- End Class
and then you would get in trouble when the user shutsdown i's computer because this won't let you close the window in any way.
But from 2.0 onwards there is a better way of doing this.
- Public Class Form1
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Me.Close()
- End Sub
- Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
- If e.CloseReason = CloseReason.UserClosing Then
- e.Cancel = True
- Me.Hide()
- End If
- End Sub
- End Class
And now when the user closes the form, either via me.close or via the closebutton of the form, the form will stay in memory. If the system closes the window like on shutdown it will just happen.



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.