469,602 Members | 1,942 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

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

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
Nov 20 '05 #1
2 2985
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.

-----Original Message-----
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 thepossibility to save the FormSettings of multiple Instances of 1 form.
I have a Form frmSource from which I have multiple Instances, each with aunique Identifier. So when I Load or Close a specific instance, I want toload or save the Settings (Top, Height, Width, etc) in an XML-file. I needto find a way to search with the unique Identifier of the Form in theXML-file.

Anybody had some examples of this? I thought maybe making a DataSet with thesettings stored in it and writing and reading the XML- file fromd theDataSet? 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
.

Nov 20 '05 #2
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
"DraguVaso" <pi**********@hotmail.com> escreveu na mensagem
news:HH*********************@phobos.telenet-ops.be...
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

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Suresh Kumaran | last post: by
1 post views Thread by john sutor | last post: by
8 posts views Thread by TJS | last post: by
6 posts views Thread by Davids | last post: by
3 posts views Thread by Yehia A.Salam | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.