When closing my app I need to save the form position and when running the app use that saved position. ie, remember where the form is located on the screen. So far I have not even figured out how to set the position by code. Any help appreciated.
Mainform.left
Mainform.top
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
See this example: depends on JavaObject and jRandomAccessFile libs.
B4X: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
Ey mikel
To get/set the form position:
B4X:Mainform.left Mainform.top
There is an event for FORM_CLOSING, here you can save the form position and load it on creation.
Sub mainForm_CloseRequest (EventData As Event)
Log (EventData)
SaveFormSettings(frmMaster)
End Sub
Sub mainForm_Closed
Log("closed")
SaveFormSettings(frmMaster)
End Sub
Sub Detail_CloseRequest (EventData As Event)
Log (EventData)
SaveFormSettings(frmDetail)
End Sub
Sub Detail_Closed
Log("closed")
SaveFormSettings(frmDetail)
End Sub