Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Very simple way to restore form window to its previous state

5.00/5 (7 votes)
9 Mar 2011CPOL 21.2K  
Very simple way to restore form window to its previous state
To restore form window to its previous state, code only, no settings usage.

VB
Public Class Form1
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Saveform()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Restoreform()
    End Sub


    Private Regpath As String = "HKEY_CURRENT_USER\Software\"
    Sub Saveform()
        My.Computer.Registry.SetValue(Regpath & My.Application.Info.Title, "WindowState", Val(Me.WindowState))
        If Me.WindowState <> FormWindowState.Normal Then
            Return
        End If
        My.Computer.Registry.SetValue(Regpath & My.Application.Info.Title, "Top", Me.Top)
        My.Computer.Registry.SetValue(Regpath & My.Application.Info.Title, "Left", Me.Left)
        My.Computer.Registry.SetValue(Regpath & My.Application.Info.Title, "Height", Me.Height)
        My.Computer.Registry.SetValue(Regpath & My.Application.Info.Title, "Width", Me.Width)
    End Sub
    Sub Restoreform()
       Me.WindowState = Val(My.Computer.Registry.GetValue(Regpath & My.Application.Info.Title, "WindowState", Me.WindowState))
        If Me.WindowState <> FormWindowState.Normal Then
            'Return 'Not needed, Remove this condition
        End If
        Me.Top = My.Computer.Registry.GetValue(Regpath & My.Application.Info.Title, "Top", Me.Top)
        Me.Left = My.Computer.Registry.GetValue(Regpath & My.Application.Info.Title, "Left", Me.Left)
        Me.Height = My.Computer.Registry.GetValue(Regpath & My.Application.Info.Title, "Height", Me.Height)
        Me.Width = My.Computer.Registry.GetValue(Regpath & My.Application.Info.Title, "Width", Me.Width)
    End Sub
End Class


Aslam Iqbal

License

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