Restore Layout
Restores the location and optionally the size of a form from a layout XML file.
This function works in conjunction with SaveLayout.
- Retrieves the layout information from a shared XML file via LoadFormLayoutXml.
- The layout is stored under a structure containing application name and form name.
- If the application or form node is not found, the form’s position is set via SetFormDefaultLocation.
- If the location or size elements are missing or invalid, the fallback is applied.
- When restoreSize is True, the size is also restored along with the position.
VB
Public Sub RestoreLayout(f As Form,
Optional restoreSize As Boolean = True)
Dim appName = GetAppNameSafe()
Dim formName = GetEffectiveFormName(f, appName)
Dim doc = LoadFormLayoutXml()
Dim root = doc.Root
If root Is Nothing Then
SetFormDefaultLocation(f)
Exit Sub
End If
Dim appNode = root.Elements("app").FirstOrDefault(Function(a) a.Attribute("name")?.Value = appName)
If appNode Is Nothing Then
SetFormDefaultLocation(f)
Exit Sub
End If
Dim formNode = appNode.Elements("form").FirstOrDefault(Function(el) el.Attribute("name")?.Value = formName)
If formNode Is Nothing Then
SetFormDefaultLocation(f)
Exit Sub
End If
Try
Dim x = Integer.Parse(formNode.Element("x")?.Value)
Dim y = Integer.Parse(formNode.Element("y")?.Value)
f.Location = New Point(x, y)
If restoreSize Then
Dim w = Integer.Parse(formNode.Element("width")?.Value)
Dim h = Integer.Parse(formNode.Element("height")?.Value)
f.Size = New Size(w, h)
End If
Catch ex As Exception
' Valori XML corrotti → fallback
SetFormDefaultLocation(f)
End Try
End Sub