The user of your application is allowed to configure certain aspects of the application to suit her preferences. You would like to save these per-user settings so that the application uses them the next time it is run.
Sample code folder: Chapter 14\UserSettings
Use the My.Settings feature of Visual Basic to enable user-and application-specific settings.
This recipe's sample code remembers the position of the form on the screen from one use to the next, and it also displays the name of the last user, which it retains in local settings.
Create a new Windows Forms application. Add a Button control named ActPrefs, and set its Text property to Preferences… Then add a Label control named UserName, and set its Text property to Your name is not set. and its UseMnemonic property to False. Adjust the form to look like Figure 14-28.
Figure 14-28. Controls on the user preferences sample
Open the Project Properties window, and select the Settings tab. This panel presents a grid of user-specific and application-specific settings. By filling in the grid, you automatically add settings that you can use in your application to retain user-preferred changes. Add two settings rows to this grid:
Leave the Scope for both settings as User, and don't provide any Value column data. Close the Project Properties window and return to the form.
Add the following source code to the form's code template:
Private Sub ActPrefs_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ActPrefs.Click ' ----- Prompt the user to change his/her preferred name. Dim newName As String newName = InputBox("Enter your name.") If (newName.Trim( ) <> "") Then ' ----- Save the user's preferences. My.Settings.PrefsUserName = newName.Trim UserName.Text = "Your name is " & newName.Trim & "." End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' ----- Display the user-defined name, if available. If (My.Settings.PrefsUserName <> "") Then UserName.Text = "Your name is " & _ My.Settings.PrefsUserName & "." End If End Sub
Return to the Form Designer, and select the form. Expand the form's (ApplicationSettings) property, and change the Location subproperty to MainFormLocation.
Run the program to test it. Each time you exit and restart the program, it remembers where you moved the form on the display. If you click the Preferences button and enter your name when prompted, it also remembers this setting the next time the program runs.
The My.Settings object is new in Visual Basic 2005. It provides a standard way to manage user-and application-specific settings. Each time the program exits, it saves any settings changes to an XML file, and it reads in that same file the next time the program runs. The exact location of this file varies, but its default location in Windows XP is:
C:\Documents and Setting\<username>\Local Settings\ Application Data\<projectname>\<specialhash>\ <version>\user.config
Application-specific settings, although not used in this sample program, are stored in an app.config file in the folder that contains your application assembly. Application-specific settings cannot be modified through the running application; you can only change them by changing the app.config file.