Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSave and restore a form's position when it loads and unloads in Visual Basic 2005
DescriptionThis example shows how to save and restore a form's position when it loads and unloads in Visual Basic 2005.
Keywordssave, restore, form, form position, Visual Basic .NET
CategoriesTips and Tricks, Controls
 
The form's Closing event handler calls subroutine SavePosition. That routine uses SaveSetting to save the form's dimensions. If the form is minimized or maximized, it saves the form's RestoreBounds settings rather than its current settings (which are not fully defined).
 
' Save the form's size and position.
Private Sub SavePosition(ByVal frm As Form, ByVal app_name _
    As String)
    SaveSetting(app_name, "Geometry", "WindowState", _
        frm.WindowState)
    If frm.WindowState = FormWindowState.Normal Then
        SaveSetting(app_name, "Geometry", "Left", frm.Left)
        SaveSetting(app_name, "Geometry", "Top", frm.Top)
        SaveSetting(app_name, "Geometry", "Width", _
            frm.Width)
        SaveSetting(app_name, "Geometry", "Height", _
            frm.Height)
    Else
        SaveSetting(app_name, "Geometry", "Left", _
            frm.RestoreBounds.Left)
        SaveSetting(app_name, "Geometry", "Top", _
            frm.RestoreBounds.Top)
        SaveSetting(app_name, "Geometry", "Width", _
            frm.RestoreBounds.Width)
        SaveSetting(app_name, "Geometry", "Height", _
            frm.RestoreBounds.Height)
    End If
End Sub
 
The form's Load event handler calls subroutine RestorePosition. That routine restores the form's size and position, and then restores the form's WindowState.
 
' Restore the form's size and position.
Private Sub RestorePosition(ByVal frm As Form, ByVal _
    app_name As String)
    frm.SetBounds( _
        GetSetting(app_name, "Geometry", "Left", _
            Me.RestoreBounds.Left), _
        GetSetting(app_name, "Geometry", "Top", _
            Me.RestoreBounds.Top), _
        GetSetting(app_name, "Geometry", "Width", _
            Me.RestoreBounds.Width), _
        GetSetting(app_name, "Geometry", "Height", _
            Me.RestoreBounds.Height) _
    )
    Me.WindowState = GetSetting(app_name, "Geometry", _
        "WindowState", Me.WindowState)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated