1. Home
  2. Computing & Technology
  3. Visual Basic

Application Settings in VB.NET
More Ways to Create an Application Setting

By Dan Mabbutt, About.com

Oct 24 2009

In addition to adding and changing settings in the Properties designer dialog to bind them to a control, you can also create application settings using the Settings tab of the Project Properties window. This lets you add settings that have nothing to do with a control (such as the location of a file) and you can also delete settings here. Select Properties under the Project menu or right-click the project in Solution Explorer and select Properties from the context menu. Here's a view showing what that looks like:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

If you add a setting here, remember to select System.Drawing.Color as the setting Type and select a color value in the Value column.

But you don't get quite the same result doing it this way. The setting isn't bound to the control (in this case, the Form). You can either go back to the Properties for the Form and bind it to the control as described earlier ... or you can change the property with code. (Event parameters are omitted to keep lines shorter.)

Private Sub Form1_Load( ...
   Me.BackColor = My.Settings.MyBackColor ' <- added
End Sub

You can also save settings that are not bound to a control. If you have a value that you need to save between executions, say, the name of the person who used your program last, you can save it using code (Sub parameters omitted to shorten lines):

Public Class Form1
   Private Sub DispLastUser_Click(ByVal ...
      LastUserDisplay.Text = My.Settings.LastUserID
   End Sub
   Private Sub LoginUser_Click(ByVal ...
      My.Settings.LastUserID = LoginID.Text
   End Sub
End Class

In our example so far, the scope of the setting has been set to User. If you code the same application with a scope of Application, you discover that the statement ...

My.Settings.myBackColor = ColorDialog1.Color

... now generates an error: "Property 'myBackColor' is ReadOnly." If you think about it, this only makes sense for an application scoped setting. It would be chaos if individual applications could change application settings. What if one application changed a database connection string while another application was connecting and disconnecting to the database? With User scoped settings, the value persisted is the one in the instance of the application that was closed last. To change these, you need to use the Settings tab in Project Properties again. (Select Properties under the Project menu or right-click the project in Solution Explorer and select Properties from the context menu.)

There is also one more feature. You can also generate a template Settings.vb file (not the Settings.Designer.vb file) by clicking the View Code button. View Code, does more than just view the code. This button also generates the file to view. The helpful comments - see the illustration below - in the generated code tell you what you need to know. Just add the code for the event you need to handle in this window.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The Settings tab of the Project Properties window is the only way to remove a setting, (select the row, right-click, and select Remove Setting).

Since the app.config file is a key part of this. The next page tells you how that works.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Application Settings in VB.NET

©2009 About.com, a part of The New York Times Company.

All rights reserved.