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.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
See also  5 Tips For Working With An Onsite Interpreter

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist