1. Computing

Application Settings in VB.NET

Clearing up confusion!

By

Updated April 16, 2014

VB.NET 1.0 introduced a sophisticated new way to save application settings like the connection strings for a database or the color of a form between executions of an application. Suppose you allow a user to customize their form with their favorite colors. You can save those preferences in a custom configuration file (Years ago, the use of .INI files was common practice.) or you can use the ability of VB.NET to save these settings. Although the architecture was part of the first release of VB.NET, VB.NET 2005 (Framework 2.0) and later uses a very different settings architecture.

Accurate information about VB.NET application settings can be hard to find. Articles in magazines and on web pages contain things that are misleading and sometimes outright false. VB.NET guru Duncan Mackenzie wrote in MSDN magazine that, "It's not all that easy to find the correct answer. You'll find a wide variety of solutions on newsgroups and forums, but only a few of the posted solutions illustrate the proper way to handle this requirement."

That's a big 10-4 there, good buddy. And it's not just newsgroups and forums. As nearly as I can tell, it's Microsoft too.

Here's an example from Microsoft's official MSDN documentation page, "Visual Studio Application Settings". (Hopefully, they will get this page updated soon, but it's been this way for over a year.)

"changes in user settings are not written to disk unless the application specifically calls a method to do so"

"user settings are automatically saved before application shutdown"

On the very same web page!

In a different MSDN article, some of the mystery is cleared up, "The settings are automatically saved only in certain project types, such as Visual Basic Windows Forms applications. In other cases, such as in Class Library projects, you will need to explicitly call the Save method of the My.Settings class." But I continue to see inconsistencies in spite of this clarification.

It's a little disturbing to see problems like this at MSDN. It's like finding out that there really isn't a Santa Claus after all. What's a programmer to do?

The goal of this article is to clear up some of that confusion. Although I've double checked everything here with my own testing, I might have made an error too. If you find one, let me know.

Another point worth knowing is that this is an area where Visual Basic's My namespace, unavailable in C#, really helps out since My.Settings is an easier way to handle settings.

Settings can be made by application, by user, or bound to a particular component (again by application or user). So there is a lot of flexibility. An example of an "application" setting might be the file location of graphics used by your company. It's the same for all users and it's set by administrators, not individual users so it's normally "read only". An example of a "user" setting might be a user ID that is saved and re-used so it doesn't have to be retyped.

On the next page, we'll create and use an a BackColor setting bound to a Form component using the VB.NET Design window just to introduce the topic. We're will be using a Windows Forms application. This all works differently with other types of applications such as WPF as well. This will be done step by step with a lot of illustrations so there will be no confusion about what's happening. The free Visual Basic 2008 Express Edition is used for all examples.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Using VB.NET
  5. Application Settings in VB.NET Using the Designer and Editing the XML Config Files

©2014 About.com. All rights reserved.