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
 

Personalization with Application Settings in Visual Basic : Page 4

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--this time in Visual Basic.


advertisement
Adding a Second Instance of UserControl1
Now, let's add another instance of UserControl1 onto the form.

Press F5 to debug the application. Select a different range of dates in each control and then shut down the application. Now, when you press F5 to start the application again, you will realize that the two controls display the same range of dates. Why? It turns out that multiple instances of the same control share the same settings class and hence there is only one set of data persisted. Solve this problem by using the SettingsKey property from the ApplicationSettingsBase class.

In the CtrlAppSettings class, add the new SettingsKey property as shown in bold below:



Public NotInheritable Class CtrlAppSettings Inherits ApplicationSettingsBase <UserScopedSetting()> _ Public Property StartDate() As Date ... End Property <UserScopedSetting()> _ Public Property EndDate() As Date ... End Property Public Overloads Property SettingsKey() As String Get Return MyBase.SettingsKey End Get Set(ByVal value As String) MyBase.SettingsKey = value End Set End Property End Class

In the Load event, assign the SettingsKey for the user control with a value so as to uniquely identify each instance of the control. The easiest way would be to set it to the name of the control, as shown in bold below:

Private Sub UserControl1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load '---can select a max. of 60 days--- MonthCalendar1.MaxSelectionCount = 60 '---set the settings key--- ctrlSettings.SettingsKey = Me.Name '---restore the MonthCalendar control to those dates saved in ' settings--- If IsDate(ctrlSettings.StartDate) Then ... End If End Sub

Press F5 to debug the application again. You will now be able to save the different range of dates for each user control.

How does the persisted application settings look like on file? For my application, it is stored in the user.config file in the following directory:

C:\Documents and Settings\<login name>\Local Settings\Application Data\AppSettings\AppSettings.vshost.exe_Url_tqftiyvv5ufraumvimzjsa53imt0ixrk\1.0.0.0

See Listing 5 to view the XML file.

Improved Usability
Knowing how to directly bind your application settings to the various properties on your Windows forms and controls and how to use the application settings feature in your custom controls will hopefully make your life simpler as you write highly customizable applications.



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