Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Navigation

Google Ads

VB.Net: Hide a form on closing.

From Wiki

Jump to: navigation, search

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

  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         Me.Close()
  5.     End Sub
  6.  
  7.  
  8.     Private Sub Form1_IsClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.IsClosing
  9.         e.Cancel = True
  10.         Me.Hide()
  11.     End Sub
  12. 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.

  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         Me.Close()
  5.     End Sub
  6.  
  7.  
  8.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  9.         If e.CloseReason = CloseReason.UserClosing Then
  10.             e.Cancel = True
  11.             Me.Hide()
  12.         End If
  13.     End Sub
  14. 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.

529 Rating: 2.3/5 (6 votes cast)