469,568 Members | 1,523 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Settings in VB.NET/WinForms

I need to write a VB.NET WinForms app. For this purpose I need to keep some
settings of the application, like many other Windows applications do.

The most obvious solution is to store settings in the XML file or registry.
If it's an XML file, then I need the following:

1. Create XSD schema, and be able to validate XML file with the settings
against that schema (How would I store the schema, if it's a class library,
for example? Would the VB.NET compiler store XSD file in a DLL?)

2. Manually create a structure that would keep all settings internally.

3. Write a routine that would parse XML file first into an XML document
object, and then into the internal structure (see step 2).

4. Bind Win Form (settings screen) to the internal structure populated with
the settings.

And likewise (to write settings)

1. Update internal settings structure with the settings from the Win Form

2. Write a routine that would store settings in the XML document, and then
flush them into a file.

Are there any other ways of storing/editing settings in VB.NET?

Thanks in advance.
Dec 10 '06 #1
5 8109
Which version? 2003 or 2005?

Robin S.
------------------
"Andy" <ak********@verizon.netwrote in message
news:_fYeh.1294$%T5.1177@trndny05...
>I need to write a VB.NET WinForms app. For this purpose I need to keep
some
settings of the application, like many other Windows applications do.

The most obvious solution is to store settings in the XML file or
registry.
If it's an XML file, then I need the following:

1. Create XSD schema, and be able to validate XML file with the
settings
against that schema (How would I store the schema, if it's a class
library,
for example? Would the VB.NET compiler store XSD file in a DLL?)

2. Manually create a structure that would keep all settings
internally.

3. Write a routine that would parse XML file first into an XML
document
object, and then into the internal structure (see step 2).

4. Bind Win Form (settings screen) to the internal structure populated
with
the settings.

And likewise (to write settings)

1. Update internal settings structure with the settings from the Win
Form

2. Write a routine that would store settings in the XML document, and
then
flush them into a file.

Are there any other ways of storing/editing settings in VB.NET?

Thanks in advance.


Dec 10 '06 #2
Dot Net version 2.0, Visual Studio 2005.

Andy.

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:jK******************************@comcast.com. ..
Which version? 2003 or 2005?

Robin S.
------------------
"Andy" <ak********@verizon.netwrote in message
news:_fYeh.1294$%T5.1177@trndny05...
I need to write a VB.NET WinForms app. For this purpose I need to keep
some
settings of the application, like many other Windows applications do.

The most obvious solution is to store settings in the XML file or
registry.
If it's an XML file, then I need the following:

1. Create XSD schema, and be able to validate XML file with the
settings
against that schema (How would I store the schema, if it's a class
library,
for example? Would the VB.NET compiler store XSD file in a DLL?)

2. Manually create a structure that would keep all settings
internally.

3. Write a routine that would parse XML file first into an XML
document
object, and then into the internal structure (see step 2).

4. Bind Win Form (settings screen) to the internal structure populated
with
the settings.

And likewise (to write settings)

1. Update internal settings structure with the settings from the Win
Form

2. Write a routine that would store settings in the XML document, and
then
flush them into a file.

Are there any other ways of storing/editing settings in VB.NET?

Thanks in advance.


Dec 10 '06 #3
You can save them to My.Settings.

Open the project properties (double-click on My Project)
and click on the Settings tab. You can enter settings here,
and then read/write them in code.

The Type should be what it really is. For example, for
form location, it should be System.Drawing.Point. The scope
is usually User.

You can set them by name:
My.Settings.DBVersion = "1.0"

To save the current values:
My.Settings.Save

You can read them and use them like this:
If my.Settings.DBVersion <"1.0" Then
'oh no! there's a problem!
call BurnDownTheHouse
End If

Application-wide settings are stored in app.config.
User settings are stored in user.config under their profile.

You can also attach settings to properties in forms and have
them be saved automatically -- look at the property
ApplicationSettings. Just be sure to put a My.Settings.Save
in your form_closing event.

I use settings to store the form size and location of all
of my forms -- the code is imbedded in my base form from
which I inherit all my other forms. If you are doing
something like this, re-post and I'll post that code;
it's about 80 lines.

Robin S.
-------------------------------

"Andy" <ak********@verizon.netwrote in message
news:nEZeh.1138$A05.41@trndny08...
Dot Net version 2.0, Visual Studio 2005.

Andy.

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:jK******************************@comcast.com. ..
>Which version? 2003 or 2005?

Robin S.
------------------
"Andy" <ak********@verizon.netwrote in message
news:_fYeh.1294$%T5.1177@trndny05...
>I need to write a VB.NET WinForms app. For this purpose I need to
keep
some
settings of the application, like many other Windows applications
do.

The most obvious solution is to store settings in the XML file or
registry.
If it's an XML file, then I need the following:

1. Create XSD schema, and be able to validate XML file with the
settings
against that schema (How would I store the schema, if it's a class
library,
for example? Would the VB.NET compiler store XSD file in a DLL?)

2. Manually create a structure that would keep all settings
internally.

3. Write a routine that would parse XML file first into an XML
document
object, and then into the internal structure (see step 2).

4. Bind Win Form (settings screen) to the internal structure
populated
with
the settings.

And likewise (to write settings)

1. Update internal settings structure with the settings from the
Win
Form

2. Write a routine that would store settings in the XML document,
and
then
flush them into a file.

Are there any other ways of storing/editing settings in VB.NET?

Thanks in advance.




Dec 11 '06 #4
Robin,

I'm definitely interested in your source code. Another question - what if
the settings could be better saved as a "tree", not as a flat
<setting><valueelements:

Normally, you would configure settings like this:

<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ClientSettings.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" />
</sectionGroup>
</configSections>

....

<userSettings>
<ClientSettings.Settings>
<setting name="setting1" serializeAs="String">
<value>True</value>
</setting>
<setting name="setting2" serializeAs="String">
<value>True</value>
</setting>
</ClientSettings.Settings>
</userSettings>

And then refer to the settings as:

ClientSettings.Settings.setting1 and ClientSettings.Settings.setting2

Whereas I need more sophisticated representation of the settings, for
example:

<userSettings>
<ClientSettings.Settings>
<Categories>
<Category Name="category1" />
<Category Name="category2" />
<Category Name="category3" />
</Categories>
<TOCEntries>
<TOCEntry Name="TOCEntry1">
<TOCEntry Name="Subentry1" />
</TOCEntry>
<TOCEntry Name="TOCEntry2" />
</TOCEntries>
</ClientSettings.Settings>
</userSettings>

Something like this. Should I write my own IConfigurationSectionHandler
class to process this?

Thanks in advance, Andy.

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:I_******************************@comcast.com. ..
You can save them to My.Settings.

Open the project properties (double-click on My Project)
and click on the Settings tab. You can enter settings here,
and then read/write them in code.

The Type should be what it really is. For example, for
form location, it should be System.Drawing.Point. The scope
is usually User.

You can set them by name:
My.Settings.DBVersion = "1.0"

To save the current values:
My.Settings.Save

You can read them and use them like this:
If my.Settings.DBVersion <"1.0" Then
'oh no! there's a problem!
call BurnDownTheHouse
End If

Application-wide settings are stored in app.config.
User settings are stored in user.config under their profile.

You can also attach settings to properties in forms and have
them be saved automatically -- look at the property
ApplicationSettings. Just be sure to put a My.Settings.Save
in your form_closing event.

I use settings to store the form size and location of all
of my forms -- the code is imbedded in my base form from
which I inherit all my other forms. If you are doing
something like this, re-post and I'll post that code;
it's about 80 lines.

Robin S.
-------------------------------

Dec 11 '06 #5
That's the general idea. The following example saves the
settings by formname, then under that is the FormLocation
and FormSize.

This is all in my base form (BaseWin):
-----------------------------------------
Private _settings As My.MySettings
Private ReadOnly Property Settings() As _
System.Configuration.ApplicationSettingsBase
Get
If _settings Is Nothing Then
_settings = New My.MySettings
End If
Return _settings
End Get
End Property

'When form closes, save the settings
Private Sub BaseWin_FormClosing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
SetSettings()
End Sub

'When form loads, apply any saved settings
Private Sub BaseWin_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
ApplySettings()
End Sub

Private Sub ApplySettings()
'Use the form name as the key for the settings
Settings.SettingsKey = Me.Name
Dim theSettings As My.MySettings
theSettings = DirectCast(Settings, My.MySettings)
If theSettings.FormSize <Drawing.Size.Empty Then
Me.Size = theSettings.FormSize
End If
If theSettings.FormLocation <Drawing.Point.Empty Then
Me.Location = theSettings.FormLocation
End If
End Sub

Private Sub SetSettings()
'Use the form name as the key for the settings
Settings.SettingsKey = Me.Name
Dim theSettings As My.MySettings
theSettings = DirectCast(Settings, My.MySettings)
If Me.WindowState = FormWindowState.Normal Then
theSettings.FormSize = Me.Size
Else
'if the form was maximized or minimized, return to the restore
state
theSettings.FormSize = Me.RestoreBounds.Size
End If
theSettings.FormLocation = Me.Location
Settings.Save()
End Sub
-----------------------------------------

Here's an example of the results:

<userSettings>
<Project1.My.MySettings.Form1>
<setting name="FormLocation" serializeAs="String">
<value>189, 2</value>
</setting>
<setting name="FormSize" serializeAs="String">
<value>434, 359</value>
</setting>
</Project1.My.MySettings.Form1>
<Project1.My.MySettings.Form2>
<setting name="FormLocation" serializeAs="String">
<value>50, 140</value>
</setting>
<setting name="FormSize" serializeAs="String">
<value>184, 190</value>
</setting>
</Project1.My.MySettings.Form2>
</userSettings>

Hopefully that will help, at least give you a starting point.
Robin S.
-------------------------------

"Andy" <ak********@verizon.netwrote in message
news:llifh.2669$_55.147@trndny09...
Robin,

I'm definitely interested in your source code. Another question - what
if
the settings could be better saved as a "tree", not as a flat
<setting><valueelements:

Normally, you would configure settings like this:

<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ClientSettings.Settings"
type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" />
</sectionGroup>
</configSections>

...

<userSettings>
<ClientSettings.Settings>
<setting name="setting1" serializeAs="String">
<value>True</value>
</setting>
<setting name="setting2" serializeAs="String">
<value>True</value>
</setting>
</ClientSettings.Settings>
</userSettings>

And then refer to the settings as:

ClientSettings.Settings.setting1 and ClientSettings.Settings.setting2

Whereas I need more sophisticated representation of the settings, for
example:

<userSettings>
<ClientSettings.Settings>
<Categories>
<Category Name="category1" />
<Category Name="category2" />
<Category Name="category3" />
</Categories>
<TOCEntries>
<TOCEntry Name="TOCEntry1">
<TOCEntry Name="Subentry1" />
</TOCEntry>
<TOCEntry Name="TOCEntry2" />
</TOCEntries>
</ClientSettings.Settings>
</userSettings>

Something like this. Should I write my own
IConfigurationSectionHandler
class to process this?

Thanks in advance, Andy.

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:I_******************************@comcast.com. ..
>You can save them to My.Settings.

Open the project properties (double-click on My Project)
and click on the Settings tab. You can enter settings here,
and then read/write them in code.

The Type should be what it really is. For example, for
form location, it should be System.Drawing.Point. The scope
is usually User.

You can set them by name:
My.Settings.DBVersion = "1.0"

To save the current values:
My.Settings.Save

You can read them and use them like this:
If my.Settings.DBVersion <"1.0" Then
'oh no! there's a problem!
call BurnDownTheHouse
End If

Application-wide settings are stored in app.config.
User settings are stored in user.config under their profile.

You can also attach settings to properties in forms and have
them be saved automatically -- look at the property
ApplicationSettings. Just be sure to put a My.Settings.Save
in your form_closing event.

I use settings to store the form size and location of all
of my forms -- the code is imbedded in my base form from
which I inherit all my other forms. If you are doing
something like this, re-post and I'll post that code;
it's about 80 lines.

Robin S.
-------------------------------


Dec 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Robb Gilmore | last post: by
2 posts views Thread by John Spiegel | last post: by
5 posts views Thread by jdecock | last post: by
13 posts views Thread by Dave | last post: by
7 posts views Thread by schoenfeld.one | last post: by
reply views Thread by =?Utf-8?B?SmFuaQ==?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.