by Brad Skidmore (4 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating:
(4 Votes)
This Procedure can be used by AnyForm to Get or Save the Form Position from the Windows Registry using SaveSetting and GetSetting :)
Inputs
pMyForm As Form
Optional pbSave As Boolean
Assumes
Best to use this in either Form_Load, Form_Unload or Form_QueryUnload
Form_Load For Getting the Saved Form Posn Settings
Unload or QueryUnload for saveing Current Form Posn.
Option Explicit
Public Sub FormWinRegPos(pMyForm As Form, Optional pbSave As Boolean)
'This Procedure will Either Retrieve or Save Form Posn values
'Best used on Form Load and Unload or QueryUnLoad
On Error GoTo EH
With pMyForm
If pbSave Then
'If Saving then do this...
'If Form was minimized or Maximized then Closed Need to Save Windowstate
'THEN... set Back to Normal Or previous non Max or Min State then Save
'Posn Parameters SaveSetting App.EXEName, .Name, "Top", .Top
SaveSetting App.EXEName, .Name, "WindowState", .WindowState
If .WindowState = vbMinimized Or .WindowState = vbMaximized Then
.WindowState = vbNormal
End If
'Save AppName...FrmName...KeyName...Value
SaveSetting App.EXEName, .Name, "Top", .Top
SaveSetting App.EXEName, .Name, "Left", .Left
SaveSetting App.EXEName, .Name, "Height", .Height
SaveSetting App.EXEName, .Name, "Width", .Width
Else
'If Not Saveing Must Be Getting ..
'Need to ref AppName...FrmName...KeyName (If nothing Stored Use The Exisiting Form value)
.Top = GetSetting(App.EXEName, .Name, "Top", .Top)
.Left = GetSetting(App.EXEName, .Name, "Left", .Left)
.Height = GetSetting(App.EXEName, Name, "Height", .Height)
.Width = GetSetting(App.EXEName, .Name, "Width", .Width)
'Be Sure WindowState is set last (Can't Change POSN if vbMinimized Or Maximized
.WindowState = GetSetting(App.EXEName, .Name, "WindowState", .WindowState)
End If
End With
Exit Sub
EH:
MsgBox "Error " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Sub
Private Sub Form_Load()
FormWinRegPos Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
FormWinRegPos Me, True
End Sub