Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
 
 
 
500MB 27GB Web Hosting - $9.95/Month
 
 
 
 
 
Old Pages
 
Old Index
Site Map
What's New
 
Books
How To
Tips & Tricks
Tutorials
Stories
Performance
Essays
Links
Q & A
New in VB6
Free Stuff
Pictures
 
 
 
TitleSave and restore a form's size and position when the program starts and stops
KeywordsSaveSetting, GetSetting, registry, position, size, persist, persistence, save, retore, Form_Load, Form_Unload
CategoriesTips and Tricks
 
When the form loads, use GetSetting to get its previous size and position. When it unloads, use SaveSetting to save the current size and position.
 
Private Const APP_NAME As String = "howto_persist_position"

Private Sub Form_Load()
    ' Restore the form's size and position.
    Me.Move _
        GetSetting(APP_NAME, "Settings", "Left", Me.Left), _
        GetSetting(APP_NAME, "Settings", "Top", Me.Top), _
        GetSetting(APP_NAME, "Settings", "Width", _
            Me.Width), _
        GetSetting(APP_NAME, "Settings", "Height", _
            Me.Height)
End Sub

Private Sub Form_Unload(Cancel As Integer)
    ' Save the form's size and position.
    SaveSetting APP_NAME, "Settings", "Left", Me.Left
    SaveSetting APP_NAME, "Settings", "Top", Me.Top
    SaveSetting APP_NAME, "Settings", "Width", Me.Width
    SaveSetting APP_NAME, "Settings", "Height", Me.Height
End Sub
 
This method is generally good enough but it has a couple of potential problems. First, it doesn't save changes unless the application ends normally. If it crashes, Form_Unload is never called so the current values are not saved. Most programmers don't count on their applications crashing but it will happen sooner or later.

Second, the values are saved in the registry. Depending on your operating system, that may mean that all uses get the same settings on the computer. For all operating systems it also means the a user moving to a different computer will not see the saved settings. If you need to worry about these issues, you can save the settings in a shared database indexed by user name.

 
 
Copyright © 1997-2003 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated