Click here to Skip to main content
Click here to Skip to main content

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

, 27 Jun 2006
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

wayvirgo
Network Administrator Kelly Tours, Inc.
United States United States
An Information Technologist for almost 20 years, now, I enjoy balancing my time between work, school, and my young family.

Comments and Discussions

 
Generalstoring 3 different strings Pinmemberrobin1smart21-Jun-07 11:27 
GeneralThank you Pinmemberjames.howell2-Jun-07 9:20 
Questionthis is 2005 does this work in 2003? PinmemberThorgalRose2-Jan-07 1:02 
AnswerRe: this is 2005 does this work in 2003? Pinmemberwayvirgo2-Jan-07 3:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 27 Jun 2006
Article Copyright 2006 by wayvirgo
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid