Code:
Public Function SaveForm(ByVal WinForm As Form) As Boolean
Dim IniDatei As XmlDocument
Dim RootNode As XmlNode
Dim Attr As XmlAttribute
Dim FormsNode As XmlNode
Dim NewFormNode As XmlNode
Dim OldFormNode As XmlNode
Dim WinStateNode As XmlNode
Dim PosNode As XmlNode
Dim SizeNode As XmlNode
Dim NodeList As XmlNodeList
Dim elem As XmlElement
Dim FormFound As Boolean Dim FormName As String = WinForm.Name
' Dummy use for OldFormNode
OldFormNode = Nothing
IniDatei = New Xml.XmlDocument
IniDatei.Load(vcst_Path & "\" & vcst_File)
' Get Forms Collection
NodeList = IniDatei.GetElementsByTagName("Form")
' Auflistung durchsuchen
For Each elem In NodeList
' Search for "Name" Attribute (Name of the Form)
If elem.HasAttribute("Name") Then
' Read the value
If elem.GetAttribute("Name") = FormName Then
' If found then exit loop
OldFormNode = elem
FormFound = True
Exit For
End If
End If
Next
' New Node for the form
NewFormNode = IniDatei.CreateNode(Xml.XmlNodeType.Element, "", "Form", "")
' append Attributes
Attr = IniDatei.CreateAttribute("Name")
Attr.Value = FormName
NewFormNode.Attributes.Append(Attr)
Attr = IniDatei.CreateAttribute("Typ")
Attr.Value = "FORM"
NewFormNode.Attributes.Append(Attr)
Attr = IniDatei.CreateAttribute("Caption")
Attr.Value = WinForm.Text
NewFormNode.Attributes.Append(Attr)
' Search the Parent Node (it's "Forms")
NodeList = IniDatei.GetElementsByTagName("Forms")
If NodeList.Count > 0 Then
' Formsnode found
FormsNode = NodeList(0)
Else
' Create the Forms Node
NodeList = IniDatei.GetElementsByTagName("Startparameter")
RootNode = NodeList(0)
FormsNode = IniDatei.CreateNode(Xml.XmlNodeType.Element, "", "Forms", "")
RootNode.AppendChild(FormsNode)
End If
If Not FormFound Then
' append new Formnode
FormsNode.AppendChild(NewFormNode)
Else
' replace the current node with the new node
FormsNode.ReplaceChild(NewFormNode, OldFormNode)
End If
' append the new values
WinStateNode = IniDatei.CreateNode(Xml.XmlNodeType.Element, "", "Winstate", "")
Attr = IniDatei.CreateAttribute("Value")
Attr.Value = WinForm.WindowState
WinStateNode.Attributes.Append(Attr)
PosNode = IniDatei.CreateNode(Xml.XmlNodeType.Element, "", "Loacation", "")
Attr = IniDatei.CreateAttribute("X")
Attr.Value = WinForm.Location.X
PosNode.Attributes.Append(Attr)
Attr = IniDatei.CreateAttribute("Y")
Attr.Value = WinForm.Location.Y
PosNode.Attributes.Append(Attr)
SizeNode = IniDatei.CreateNode(Xml.XmlNodeType.Element, "", "Size", "")
Attr = IniDatei.CreateAttribute("Width")
Attr.Value = WinForm.Size.Width
SizeNode.Attributes.Append(Attr)
Attr = IniDatei.CreateAttribute("Height")
Attr.Value = WinForm.Size.Height
SizeNode.Attributes.Append(Attr)
' append the valuenodes
NewFormNode.AppendChild(WinStateNode)
NewFormNode.AppendChild(PosNode)
NewFormNode.AppendChild(SizeNode)
' save file
IniDatei.Save(vcst_Path & "\" & vcst_File)
End Function