devxlogo

Save Form Position and Size Using SaveSetting

Save Form Position and Size Using SaveSetting

SaveSetting and GetSetting make writing application settings a breeze. These two utility functions retrieve andstore the current forms position:

 Public Sub FormPosition_Get(F As Form)' Retrieve Form F's position from an ' ini/reg file and position it ' accordinglyDim buf As StringDim l As Integer, t As IntegerDim h As Integer, w As IntegerDim pos As Integerbuf = GetSetting(app.EXEName, _        "FormPosition", F.Tag, "")If buf = "" Then         ' defaults to centering the form        F.Move (Screen.Width - F.Width)  _                2, (Screen.Height - F.Height)  2Else        ' extract l,t,w,h and move the form        pos = InStr(buf, ",")        l = CInt(Left(buf, pos - 1))        buf = Mid(buf, pos + 1)        pos = InStr(buf, ",")        t = CInt(Left(buf, pos - 1))        buf = Mid(buf, pos + 1)        pos = InStr(buf, ",")        w = CInt(Left(buf, pos - 1))        h = CInt(Mid(buf, pos + 1))        F.Move l, t, w, hEnd IfEnd SubPublic Sub FormPosition_Put(F As Form)' Write form F's top,left,height and ' width properties to the reg/ini file ' for the applicationDim buf As Stringbuf = F.left & "," & F.top & "," & _        F.Width & "," & F.HeightSaveSetting app.EXEName,_        "FormPosition", F.Tag, bufEnd Sub

You should place these routines in a module and call them from the forms’ Load and Unload events. You mustplace the name of the form in its Tag property for these utilities to work:

 Sub Form_Load()        FormPosition_Get MeEnd SubSub Form_Unload()        FormPosition_Put MeEnd Sub

devx-admin

Share the Post: