Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Example usage of the new Visual Studio 2005 My.Settings object

0.00/5 (No votes)
27 Jun 2006 1  
This articles demonstrates the storage and retrieval of settings in the user configuration file via the new My.Settings object integrated into the VS2005 development environment. In the process, I also explore the usage of the ListView component, and demonstrate how to add and read columns from it.

Introduction

This is a simple application that I wrote to explore some of the new capabilities of the Visual Studio 2005 framework, and serves to demonstrate several handy features.

The two primary features shown are direct binding of a control (the form itself, in this case) to the settings object, and manually reading and writing to and from the object with code.

Background

With Visual Studio 2005, Microsoft has introduced a new object, called "My", which simplifies many common tasks such as file access, setting and getting user and application information, and much more. The specific object I'm working with in this demo app is the "My.Settings" object, which has links to an XML file in the user directory, under Documents & Settings. This way, information saved is in a somewhat obscure location, for security, and is unique per user, so that each can have their own settings for a given application. Access to this file is made completely transparent to the developer, so using it is extraordinarily easy!

To use these settings in your own project, you must first go to the Properties page for your project, and configure each property that you want to use, like so:

In this example, you'll see that I have three properties, scoped solely to the user, which have three different data types:

  1. TextItems: Specialized.StringCollection: A list to hold some test data.
  2. Size: System.Drawing.Size: A single variable that holds the form's size, via bindings.
  3. Location: System.Drawing.Point: Similar to "Size".

I'm pretty sure both Size and Location are of type "point", but the property bindings, for some reason, specifically require those types, and Visual Studio will only expose objects of the exact proper type.

Note: I'm pretty sure that you must manually edit you StringCollection once via the Value field, above, which creates the XML that you see in the dialog above. You may not be able to use your StringCollection values until you first manually add some type of test value.

Seen below, is the bindings property for the location of the form:

To bind the size of the form, you must click on the "..." (ellipsis) for advanced settings in the (PropertyBinding) field, just above "Location", and then find the "ClientSize" property:

Having set the bindings for the primary form, it will now remember its size and location on the screen, with no programming required!

Using the code

Now that the application's user settings have been configured, they are quite easy to use programmatically, as shown by this code, which enumerates the ListView control and pumps the text values into the Settings object:

' Loop through each item displayed in the ListView Control

For Each i As ListViewItem In Me.ListView1.Items
    ' First check to see if the item already exists

    If Not My.Settings.TextItems.Contains(i.Text) Then
        ' Add to the list to be saved for later.

        My.Settings.TextItems.Add(i.Text)
    End If
Next

Reading the values back from the object is just as easy:

For Each str As String In My.Settings.TextItems
    Me.ListView1.Items.Add(str)
Next

Points of interest

An interesting point about the code above is that it uses a "ListViewItem" as the return object type for the ListView1.Items collection. While not immediately intuitive, this object is useful, and allows full access to the contents of the ListView, and even allows you to make columnar displays, much like a DataGrid.

History

  • Version 1.0.0.0 - created and posted on 6/27/2006.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here