April 9, 2006 : Settings That Save Themselves
© 安岡喜晴 (JoyShine)
Writing the code to save settings was a chore.
Keep what is in a text box, and put it back when the program opens next time.
That is all it is, and yet one more control meant one more set of the same lines.
I made it so that passing the control is enough.
The key in the XML is the name of the control itself.
The form already holds that name, so there is nothing for me to decide.
Public Function TextBoxOperatorSaveTextBoxData(ByVal TargetTextBox As TextBox) As Boolean
Try
Me.SetElement2(TargetTextBox.Name & "/" & Me.StringConverterMethod(TargetTextBox.Text))
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Function TextBoxOperatorLoadTextBoxData(ByVal TargetTextBox As TextBox) As Boolean
Try
TargetTextBox.Text = Me.StringConverterMethod(Me.GetXMLValue(TargetTextBox.Name))
Return True
Catch ex As Exception
Return False
End Try
End Function
What is passed is a single string, and the levels that are not there yet are made as it goes.
Public Function SetElement2(ByVal Value As String)
Dim EXD As XmlDocument
Dim EXN As XmlNode
Dim Ar() As String
Dim x As Integer
Dim ELE As XmlElement
Dim ELETEXT As XmlText
Value = Me.CompanyName & "/" & SoftwareName & "/" & Value
EXD = New XmlDocument
EXD.Load(Me.InputFile)
Ar = Split(Value, "/")
For x = 0 To Ar.Length - 2
If Me.ElementExist(Ar(x)) = True Then
If x = Ar.Length - 2 Then
EXD.GetElementsByTagName(Ar(x)).Item(0).InnerText = Ar(x + 1)
EXD.Save(OutputFile)
End If
Else
EXN = EXD.GetElementsByTagName(Ar(x - 1)).Item(0)
ELE = EXD.CreateElement(Ar(x))
If x = Ar.Length - 2 Then
ELETEXT = EXD.CreateTextNode(Ar(Ar.Length - 1))
Else
ELETEXT = EXD.CreateTextNode(String.Empty)
End If
ELE.AppendChild(ELETEXT)
EXN.AppendChild(ELE)
EXD.Save(Me.OutputFile)
EXD.Load(Me.OutputFile)
End If
Next
Me.FinalizeMethod()
End Function
Every save rewrites the file.
Check boxes and list boxes are written the same way.