Store last position and size of every form

By | 2019-08-29

Use this code to store last position and size of every form in your project, every form will be loaded in the state and position of the last time it was opened.

Use this code to store last position and size of every form in your project every form will be loaded in the state and position of the last time it was opened since it uses App.Title and Me.Name to store values in the registry it can be used “as is” just by pasting it into your forms’ Load and Unload events.

Private Sub Form_Load()
   'load settings from the registry
   Me.WindowState = GetSetting(App.Title, Me.Name, "WindowState", Me.WindowState)
   If Me.WindowState = vbNormal Then
      Me.Width = GetSetting(App.Title, Me.Name, "Width", Me.Width)
      Me.Height = GetSetting(App.Title, Me.Name, "Height", Me.Height)
      Me.Left = GetSetting(App.Title, Me.Name, "Left", Me.Left)
      Me.Top = GetSetting(App.Title, Me.Name, "Top", Me.Top)
   End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
   'Save orm dimensions and state in the registry
   SaveSetting App.Title, Me.Name, "WindowState", Me.WindowState
   If Me.WindowState = vbNormal Then
      SaveSetting App.Title, Me.Name, "Height", Me.Height
      SaveSetting App.Title, Me.Name, "Width", Me.Width
      SaveSetting App.Title, Me.Name, "Left", Me.Left
      SaveSetting App.Title, Me.Name, "Top", Me.Top
   End If
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.