Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Use Windows Forms Application Settings to Personalize Your Applications : Page 3

Application Settings is a new feature in Windows Forms 2.0 that allows you to maintain both user- and application-specific data (known as settings). Find out how to use it to improve your application's usability with personalization.


advertisement
Adding Collections
The previous examples showed how to add settings of relatively simple types. One of the improvements in .NET 2.0 is the ability to add settings of complex types, such as adding a collection of strings. For example, suppose you want to store a collection of strings into a setting. Here's how you can do it.

Add the MyItems setting. In the Type dropdown list, select Browse…

The Select a Type dialog will appear. Type the following in the textbox:



System.Collections.Specialized.StringCollection

Then click OK. Next, click the “…” button on the Value field. Enter the strings collection and click OK.

The MyItems application settings will now be represented in the app.config file as follows:

<setting name="MyItems" serializeAs="Xml"> <value> <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <string>Item 1</string> <string>Item 2</string> <string>Item 3</string> <string>Item 4</string> <string>Item 5</string> </ArrayOfString> </value> </setting>

To access the string collections programmatically, use the following code snippet:

foreach (string str in Properties.Settings.Default.MyItems) { Console.WriteLine(str); }

Separating Application Settings
If you have a large number of application settings to add to your project, it is a good idea to store them separately in different files instead of storing them all in the default Settings.settings file. This also makes it easier to delete a related group of settings without affecting the others.

To add a settings file to your project, right-click on the project name in Solution Explorer and select Add | New Item…. Select the Settings File template and name it as, say, UI.Settings. Click Add.

The UI.settings file will now be added to your project. Double-click to open it. As usual, add an application setting. In this case, I have added a Title setting (see Figure 7).


Figure 7. The Title Setting: Defining a new setting.
 
Figure 8. The Properties Folder: Moving the settings file.

The next step is to move the UI.settings file to the Properties folder of the project (see Figure 8) so that you can access it programmatically under the Properties class.

You can now access the Title setting using the code:

this.Text = Properties.UI.Default.Title;

Note that all the settings from the different settings files are still stored in the same app.config file (design time). Also remember that, during runtime, all the application settings values are stored in the same user.config file.

<applicationSettings> vs. <appSettings>
In .NET 1.x, you can add application settings to your app.config file, like this:

<appSettings> <add key="FileName" value="C:\TextFile.txt"/> </appSettings>

So how does this differ from .NET 2.0? Well, the major advantage of the application settings feature in .NET 2.0 is that all the application settings are strongly typed, while those defined using the <appSettings> element are not. And this greatly improves on your code safety.

And in .NET 1.x, you use the ConfigurationSettings class to access the settings stored under the <appSettings> element:

using System.Configuration; ... ConfigurationSettings.AppSettings.Get("FileName").ToString();

Figure 9. Warning Message: This alerts you to the obsolete ConfigurationSettings class.

While the ConfigurationSettings class is still supported in .NET 2.0, it is obsolete. You will get the warning message shown in Figure 9 if you use it in Visual Studio 2005.

The "System.Configuration!" indicates that you need to add a reference to the System.Configuration.dll library.

In .NET 2.0, use the ConfigurationManager class to access the setting, like this:

using System.Configuration; ... ConfigurationManager.AppSettings.Get("FileName").ToString();

Improved Usability
Now you know how settings are created and used, and you should also have a good idea of the differences between application-scope and user-scope settings. Using settings to personalize your applications can really improve their usability, so it's in your interest to make full use of them.



Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date