Home All Groups Group Topic Archive Search About

Settings in VB.NET/WinForms

Author
10 Dec 2006 6:22 PM
Andy
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.

Author
10 Dec 2006 7:32 PM
RobinS
Which version? 2003 or 2005?

Robin S.
------------------
"Andy" <akorney***@verizon.net> wrote in message
news:_fYeh.1294$%T5.1177@trndny05...
Show quoteHide quote
>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.
>
>
Author
10 Dec 2006 7:57 PM
Andy
Dot Net version 2.0, Visual Studio 2005.

Andy.

Show quoteHide quote
"RobinS" <RobinS@NoSpam.yah.none> wrote in message
news:jKednQ8fa-Na_eHYnZ2dnUVZ_tCtnZ2d@comcast.com...
> Which version? 2003 or 2005?
>
> Robin S.
> ------------------
> "Andy" <akorney***@verizon.net> wrote 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.
> >
> >
>
>
Author
11 Dec 2006 3:57 AM
RobinS
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" <akorney***@verizon.net> wrote in message
news:nEZeh.1138$A05.41@trndny08...
Show quoteHide quote
> Dot Net version 2.0, Visual Studio 2005.
>
> Andy.
>
> "RobinS" <RobinS@NoSpam.yah.none> wrote in message
> news:jKednQ8fa-Na_eHYnZ2dnUVZ_tCtnZ2d@comcast.com...
>> Which version? 2003 or 2005?
>>
>> Robin S.
>> ------------------
>> "Andy" <akorney***@verizon.net> wrote 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.
>> >
>> >
>>
>>
>
>
Author
11 Dec 2006 7:30 PM
Andy
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><value> elements:

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.

Show quoteHide quote
"RobinS" <RobinS@NoSpam.yah.none> wrote in message
news:I_2dnUUVWoisSuHYnZ2dnUVZ_rKvnZ2d@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.
> -------------------------------
Author
11 Dec 2006 9:57 PM
RobinS
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" <akorney***@verizon.net> wrote in message
news:llifh.2669$_55.147@trndny09...
Show quoteHide quote
> 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><value> elements:
>
> 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" <RobinS@NoSpam.yah.none> wrote in message
> news:I_2dnUUVWoisSuHYnZ2dnUVZ_rKvnZ2d@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.
>> -------------------------------
>
>