B4J Question Save and recall a form position specs

Mikelgiles

Active Member
Licensed User
Longtime User
I need to save and later store Mainform.WindowLeft, Top, width, & height. Need to do this in Sub or Function. I want the user to be able move the window (even to s secondary monitor if they want) and have the app put it back where they put it. I am guessing that this would be a pretty popular need and am hoping that someone knows how

I get a error "Object Expected" on the second line.

B4X:
    Dim MainFrmLTWH As String
    MainFrmLTWH = MainForm.WindowLeft.string + "~" + _
                               MainForm.Windowtop.string + "~" + _
                               MainForm.Windowwidth.string + "~" + _
                               MainForm.Windowheight.string + "~"
 

stevel05

Expert
Licensed User
Longtime User
string is not a method of MainForm.WindowLeft (or WindowTop or WindowWidth or WindowHeight) and the string concatenation operator is & and not +.

I store the values in a map and write that to wherever I am storing app data. Usually a keyvaluestore, or if it's a small app just save the map.

Using these two subs Opts is a map global variable:

B4X:
Public Sub SaveFormMetrics(F As Form, Delim As String)
    If F.Title = "Form" Then
        Log(F.Title & " Cannot set form metrics for a form with an unmodified title")
        Return
    End If
    Dim FormName As String = F.Title
    If FormName.Contains(Delim) Then
        FormName = FormName.SubString2(0,FormName.IndexOf(Delim)).Trim
    End If
    Opts.Put("FormPos" & FormName & "Left",F.WindowLeft)
    Opts.Put("FormPos" & FormName & "Top",F.WindowTop)
    Opts.Put("FormPos" & FormName & "Width",F.WindowWidth)
    Opts.Put("FormPos" & FormName & "Height",F.WindowHeight)
End Sub

Public Sub SetFormMetrics(F As Form,Delim As String)
    Dim FormName As String = F.Title
    If FormName.Contains(Delim) Then
        FormName = FormName.SubString2(0,FormName.IndexOf(Delim)).Trim
    End If
    If Opts.ContainsKey("FormPos" & FormName & "Left") Then
        F.WindowLeft = Opts.Get("FormPos" & FormName & "Left")
        F.WindowTop = Opts.Get("FormPos" & FormName & "Top")
        F.WindowWidth = Opts.Get("FormPos" & FormName & "Width")
        F.WindowHeight = Opts.Get("FormPos" & FormName & "Height")
    End If
End Sub

The form title is checked for a delimiter in case I want to change it during the program for any reason, the name stored is anything before the delimiter.
 
Last edited:
Upvote 1
Top