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:
TextItems
: Specialized.StringCollection
: A list to hold some test data.
Size
: System.Drawing.Size
: A single variable that holds the form's size, via bindings.
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:
For Each i As ListViewItem In Me.ListView1.Items
If Not My.Settings.TextItems.Contains(i.Text) Then
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.