473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 8353
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robb Gilmore | last post by:
posting this again as : rgilmore@newsgroup.nospam We have a C# .NET app which runs as a service. It posts data to a server using HttpWebrequest. According to the docs I 've read, it should use...
19
by: Linda | last post by:
In classic ASP I used to have a file called settings.asp included on every page of my web, it consisted of a number of different settings unique to this application, among them the database path...
2
by: John Spiegel | last post by:
Hi all, I'm looking for suggestions on storing classifications of application settings in a WinForms app. I've got a couple ideas using XML config files. One is to have a set of .config files...
5
by: jdecock | last post by:
I'm fairly new to winforms and I'm having a hard time creating a settings dialog. I'd like to have a list on the left side of the dialog and show a different set of controls on the left based on...
4
by: Richard Lewis Haggard | last post by:
I have an application that can't use the registry to save various user options and application settings. My first thought was to simply use an application configuration file but this approach seems...
13
by: Dave | last post by:
When using the properties designer to store application wide properties how do you get this to work across a project group containing an EXE and a collection of DLLs. I'm using C#.Net 2005. I...
7
by: schoenfeld.one | last post by:
How are Application Settings supposed to be used in multi-tiered applications? If I have a winforms application consising of 2 projects 1. UI PROJECT: has the settings 2. DATABASE...
2
by: jtw | last post by:
I am trying to read an application setting from my App.Config file that I have modified from the default value that was entered into the settings window under project properties. string myConn =...
0
by: =?Utf-8?B?SmFuaQ==?= | last post by:
Hi parez, I agree with Jon, for this case are user settings the best solution. The article http://www.codeproject.com/KB/dotnet/user_settings.aspx describes the pros. Jani "parez" wrote:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.