Click here to Skip to main content
15,861,367 members
Articles / Programming Languages / Visual Basic
Alternative
Tip/Trick

Very simple way to restore form window to its previous state

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
20 Mar 2011CPOL 9.6K   2   3
Here is a way to do this without using the registry.In the project's properties, Settings pane, add the following settings. You can name these settings anything you want, but use the same setting name in the code shown below:Name: MainWindowStateType:...
Here is a way to do this without using the registry.
In the project's properties, Settings pane, add the following settings. You can name these settings anything you want, but use the same setting name in the code shown below:
Name: MainWindowState
Type: System.Windows.Forms.FormWindowState
Scope: User
-----
Name: MainWindowLocation
Type: System.Drawing.Point
Scope: User
-----
Name: MainWindowSize
Type: System.Drawing.Size
Scope: User

Put the following in the form's Load event:
Try
    Dim st As FormWindowState = My.Settings.MainWindowState
    Dim siz As Size = My.Settings.MainWindowSize
    Dim loc As Point = My.Settings.MainWindowLocation
    Me.WindowState = st
    If Me.WindowState = FormWindowState.Normal Then
        If siz.Width <> 0 AndAlso siz.Height <> 0 Then Me.Size = siz
        If loc.X <> 0 AndAlso loc.Y <> 0 Then Me.Location = loc
    End If

Catch ex As Exception
    Me.Location = New Point(100, 100)
End Try

Put the following in the form's FormClosing event:
My.Settings.MainWindowState = Me.WindowState
If Me.WindowState = FormWindowState.Normal Then
    My.Settings.MainWindowLocation = Me.Location
    My.Settings.MainWindowSize = Me.Size
End If
My.Settings.Save()

Add settings for as many forms as you want to track in your project, and adjust the code accordingly. Put the code blocks in each form, referencing the proper setting name. Each user will then have the forms appear exactly as he/she left them the last time.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalnice answer I prefer to steer away from the registry as much... Pin
Simon_Whale6-Jun-11 1:34
Simon_Whale6-Jun-11 1:34 
GeneralYou could also use Xml file to accomplish this Pin
charles henington20-Mar-11 20:46
charles henington20-Mar-11 20:46 
GeneralI prefer the settings method as well. This allows for bette... Pin
TheyCallMeMrJames20-Mar-11 11:30
TheyCallMeMrJames20-Mar-11 11:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.