Class: FormPos in Category Microsoft Visual Basic 6 : Forms from Total Visual SourceBook

A form class to position a form on a screen in VB6.

Easily save a form's position to the Windows registry, then restore it to its previously saved position. You may also center a form or move it to a specified location. This class uses the Windows registry location based on the specified RegistryPath and RootKey properties.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CFormPos class.
Form Property Get a pointer to the form that was previously assigned to this property.
RegistryPath Property Get the current value of the RegistryPath used for form position entries in the registry (defaults to SOFTWARE\ plus the value of App.Title).
RootKey Property Get the current value of the RootKey used for the form position entries in the registry (defaults to fprkHKeyLocalMachine, or the "HKEY_LOCAL_MACHINE" root key hive of the registry).
SubKey Property Get the current value of the SubKey used for the form position entries in the registry. If not supplied, or set to "", the entries are stored in the root of the key indicated by the RegistryPath property.
Class_Initialize Initialize Set initial values to defaults which may be overridden with property settings.
Class_Terminate Terminate Release resources used by the class.
CenterForm Method Centers the form on the screen.
RestoreForm Method Restores the form to the last-saved position stored by the SaveForm method. Use the fForceVisible argument to handle the case where the form position was saved at a high resolution, but the user then changed to a low-resolution.
SaveForm Method Saves the current position of the form to the Windows registry. This method stores the form's current Left, Top, Width, and Height values, along with its current WindowState property, to the Windows registry. The location where this value is saved is determined by the RootKey, RegistryPath and SubKey properties of the class.
RegistryGetKeyString Private Get a string value from the system registry.
RegistrySetKeyValue Private Set a key value.
' Example of the CFormPos class
'
' To try this example, do the following:
' 1. Create a new form.
'    Leave the form's StartupPosition property set to 3 - Windows Default
' 2. Paste all the code from this example to the new form's module
' 3. Run the form. If the form's previous position was saved, it is restored on open.
'    When the form is closed its position is saved.

' In the Declarations section of the form define the variable
Private mFormPos As CFormPos

Private Sub Form_Load()

  ' Instantiate variable
  Set mFormPos = New CFormPos

  ' Assign current form to the Form property of the object
  Set mFormPos.Form = Me

  ' Assign alternative value to the AppName property
  mFormPos.RegistryPath = "SOFTWARE\FMS\Test CFormPos"
  mFormPos.SubKey = "Form Positions"

  ' Restore to previously saved locations. If not previously saved, then simply center the form
  If Not mFormPos.RestoreForm Then
    mFormPos.CenterForm
  End If

End Sub

Private Sub Form_Unload(Cancel As Integer)

  ' Save current position for next time
  mFormPos.SaveForm

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS