### Form Settings

Example save and load settings.
Libraries required: JavaObject and jRandomAccessFile
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private SettingsFolder As String
Private Const SettingsFile As String = "settings.dat"
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
SettingsFolder = File.DirData("MyApp")
MainForm = Form1
LoadSettings
MainForm.Show
End Sub`
`Sub LoadSettings
If File.Exists(SettingsFolder, SettingsFile) Then
Try
Dim r As RandomAccessFile
r.Initialize(SettingsFolder, SettingsFile, True)
Dim m As Map = r.ReadB4XObject(r.CurrentPosition)
SetFormFromMap(m, MainForm)
Catch
Log(LastException)
End Try
End If
End Sub`
`Sub MainForm_Closed
Dim r As RandomAccessFile
r.Initialize(SettingsFolder, SettingsFile, False)
r.WriteB4XObject(FormToMap(MainForm), 0)
r.Close
End Sub`
`Sub SetFormFromMap(m As Map, f As Form)
f.WindowLeft = m.Get("left")
f.WindowTop = m.Get("top")
f.WindowWidth = m.Get("width")
f.WindowHeight = m.Get("height")
Dim iconified As Boolean = m.Get("iconified")
If iconified Then
Dim jo As JavaObject = f
jo.GetFieldJO("stage").RunMethod("setIconified", Array(iconified))
End If
'check that left and top are in screen boundaries
Dim goodLeft, goodTop As Boolean
For Each screen As Screen In fx.Screens
If f.WindowLeft >= screen.MinX And f.WindowLeft <= screen.MaxX Then
goodLeft = True
End If
If f.WindowTop >= screen.MinY And f.WindowTop <= screen.MaxY Then
goodTop = True
End If
Next
If Not(goodLeft) Then f.WindowLeft = 0
If Not(goodTop) Then f.WindowTop = 0
End Sub`
`Sub FormToMap (f As Form) As Map
Dim m As Map = CreateMap("left": f.WindowLeft, "top": f.WindowTop, "width": f.WindowWidth, _
"height": f.WindowHeight)
Dim jo As JavaObject = f
m.Put("iconified": jo.GetFieldJO("stage").RunMethod("isIconified", Null))
Return m
End Sub`

Changed: , Category: Forms, File: T6C195M1418.htm, ID: 1418