Save FormSettings of MULTIPLE Forms in XML file!? (VB.NET)

D

DraguVaso

Hi,

I found some examples for storing the FormSettings of a Form in an XML-file,
but none of these could match my criteria: What I am looking for is the
possibility to save the FormSettings of multiple Instances of 1 form.

I have a Form frmSource from which I have multiple Instances, each with a
unique Identifier. So when I Load or Close a specific instance, I want to
load or save the Settings (Top, Height, Width, etc) in an XML-file. I need
to find a way to search with the unique Identifier of the Form in the
XML-file.

Anybody had some examples of this? I thought maybe making a DataSet with the
settings stored in it and writing and reading the XML-file fromd the
DataSet? But how do I fill that DataSet than with my FormSettings?

I'm pretty new to XML (and VB.NET also), so any help would be appreciated!
Thanks in advance!!

DraguVaso
 
G

Guest

Create a dataset sounds like a good idea, and then you
could persist this on program shutdown and reopen when the
program opens. You could also define a class that had
properties corresponding to the form attributes you wanted
to keep track of and serialize/ deserialize this when the
program closed/ opened.

With respect to the dataset method you could try the
following:
Add a schema to your project and define a table with
column headings corresponding to the form attributes you
want to record.

Build a dataset based on the above defined schema.

To Add Initialisation Data To DataSet
Dim NewDataRow As DataRow
For Each FormToRecord
MyNewDataRow = MyFormDataSet.Tables(0).NewRow
MyNewDataRow(ColumnIndexes)= PropertyValues
MyFormDataSet.Tables(0).Add(NewRow)
Next

Create a procedures to call from open and close form
events. Example close event is given below:

Sub FormClose(ByVal FormBeingClosed As Form)

Dim EndLoop As Boolean= False
IENumRow = MyFormDataSet.Tables
(0).Rows.GetEnumerator 'Assumes Only One Row
Do While IENumRow.MoveNext Or Not EndLoop
If IENumRow.Current(Index Corresponding to
column containing identifier) = FormBeingClosedID Then
IEnumRow.Current(IndexOfProperty1Column)=
FormBeingClosed.RelevantProperty
EndLoop= True
End If
Loop
'Use The Index To Update The Current DataRow
MyFormDataSet.Tables(0).Row(Count)
on the open and load events for each form.
 
M

Marcos Ribeiro

I use the class (code bellow) to write keys on app.exe.config thats is
loaded at startup
You can choose under what key you want to store any settings (set Identifier
from de calling routine)
One remark: While developing it writes on app.exe.config but try to not load
setings on the app.config so i'll need to manuallly copy settings on
app.exe.config to app.config to work on IDE
Hope it Helps
Marcos
Form Code:
*****************
Imports System.Configuration
Imports myApp.myUtilities
Public Identifier as Integer
Load....
Me.Width = CType(Reader.GetValue(Identifier.tostring & ".Width",
GetType(System.Int32)), Int32)
end sub

Closing....
Dim Writer As New AppSettingsWriter()
Writer(Identifier.tostring & ".Width") = CStr(Me.Width)
Writer.SaveFile()
end sub

Class:
*****************
Imports System
Imports System.Xml

Namespace myApp.myUtilities
Public Class AppSettingsWriter
Private configFileName As String
Private document As XmlDocument

Public Sub New()
Dim asmy As System.Reflection.Assembly
Dim tempName As String
asmy = System.Reflection.Assembly.GetEntryAssembly()
tempName = asmy.Location
configFileName = tempName & ".config"
document = New XmlDocument()
document.Load(configFileName)
End Sub

Default Public WriteOnly Property Value(ByVal key As String) As String
Set(ByVal Value As String)
Dim Query As String
Dim Node As XmlNode
Dim Root As XmlNode
Dim Attribute1 As XmlNode
Dim Attribute2 As XmlNode

Query = "/configuration/appSettings/add[@key=" & _
Chr(34) & key & Chr(34) & "]"
Node = document.DocumentElement.SelectSingleNode(Query)
If Not Node Is Nothing Then
Node.Attributes.GetNamedItem("value").Value = Value
Else
Node = document.CreateNode(XmlNodeType.Element, "add", "")
Attribute1 = document.CreateNode(XmlNodeType.Attribute, "key", "")
Attribute1.Value = key
Node.Attributes.SetNamedItem(Attribute1)
Attribute2 = document.CreateNode(XmlNodeType.Attribute, "value", "")
Attribute2.Value = Value
Node.Attributes.SetNamedItem(Attribute2)
Query = "/configuration/appSettings"
Root = document.DocumentElement.SelectSingleNode(Query)
If Not Root Is Nothing Then
Root.AppendChild(Node)
Else
Throw New InvalidOperationException("Não pude adicionar o nodo ao
arquivo config")
End If
End If
End Set
End Property

Public Sub SaveFile()
document.Save(configFileName)
End Sub
End Class
End Namespace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

xml serializer bug 6
What to search for 8
XmlNode for dataset xml 1
Convert XML schema to DataSet in VB Express 2008? 0
Settings in VB.NET/WinForms 5
dataset xml 3
VB.Net and XML 6
xml method 2

Top