Click here to Skip to main content
15,881,832 members
Articles / Desktop Programming / Windows Forms
Article

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

Rate me:
Please Sign up or sign in to vote.
2.77/5 (9 votes)
27 Jun 2006CPOL3 min read 43.4K   258   25   4
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.

Image 1

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:

Image 2

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:

Image 3

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:

Image 4

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:

VB
' 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:

VB
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)


Written By
Network Administrator At Large
United States United States
An Information Technologist for 20+ years, now, I enjoy balancing my time between work, play, and family.

Comments and Discussions

 
Generalstoring 3 different strings Pin
robin1smart21-Jun-07 11:27
robin1smart21-Jun-07 11:27 
GeneralThank you Pin
james.howell2-Jun-07 9:20
james.howell2-Jun-07 9:20 
This article has saved me a lot of headaches. I am throwing together a simple remote desktop tool to save myself time at work, and was attempting to learn xml serialization, but now I can just have the items in a listview and save to my.settings, which has the added advantage I can have icons for each of the servers if I want to.

Thank you for helping a n00b Laugh | :laugh:
Questionthis is 2005 does this work in 2003? Pin
ThorgalRose2-Jan-07 1:02
ThorgalRose2-Jan-07 1:02 
AnswerRe: this is 2005 does this work in 2003? Pin
wayvirgo2-Jan-07 3:44
wayvirgo2-Jan-07 3:44 

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

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