Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



Component One


rssbus
 


Xojo

Reader rating:
Click here to read 4 comments about this article.
Article source: CoDe (2006 - Jul/Aug)


Article Pages: < Previous - 1  2  3 - Next >


Retaining Multiple Sets of User Settings (Cont.)

For form-specific user preferences, you can set the value of the setting before the form is closed. For the form size example, set the FormSize setting before closing the form as follows:

Private Sub CustomerWin_FormClosing(ByVal sender _
   As ObjectByVal e _
   As System.Windows.Forms.FormClosingEventArgs) _
   Handles Me.FormClosing
  If Me.WindowState = FormWindowState.Normal Then
    My.Settings.FormSize = Me.Size
  Else
    ' If the form was maximized or minimized,
    ' return to the restore state
    My.Settings.FormSize = Me.RestoreBounds.Size
  End If
End Sub

This code first checks the form state. If the WindowState is normal, you can simply store the form size. But if the WindowState is any other state, such as minimized or maximized, you must use the RestoreBounds size. Additionally, you could define a setting for WindowState so you can return the form to its original state.

You restore the user setting when the form is opened:

Private Sub CustomerWin_Load(ByVal sender As _
   ObjectByVal As System.EventArgs) _
   Handles Me.Load
  If My.Settings.FormSize <_
           System.Drawing.Size.Empty Then
    Me.Size = My.Settings.FormSize
  End If
End Sub

The settings are stored in two XML files: an app.config file, which is created at design time when you create the first application setting; and a user.config file, which is created at runtime when the user changes the value of any user setting.

The user.config file is stored in:

C:\Documents and Settings\<your username>\
Local Settings\Application Data

This is called the local user profile. Alternatively, it will be stored in:

C:\Documents and Settings\
<your username>\Application Data

This is called the roaming user profile.

Note: If you use ClickOnce deployment, the settings will be saved in the ClickOnce data directory and not in the local user configuration file.

The Visual Basic runtime automatically saves the user.config settings when the application is closed and retrieves the settings when the application is opened again.

The userSettings section of the user.config XML file for the two settings defined here looks like this:

<userSettings>
    <PTWin.My.MySettings>
        <setting name="FormLocation"
                           serializeAs="String">
            <value>57, 11</value>
        </setting>
        <setting name="FormSize" 
                           serializeAs="String">
            <value>566, 362</value>
        </setting>
    </PTWin.My.MySettings>
</userSettings>

Use these new settings features whenever you have application properties that you don’t want to hard-code into the application and when you want to store and retrieve a single set of user preferences.

Retaining Multiple Sets of User Settings

As currently defined, the FormLocation and FormSize example settings will only work for one form. For a consistent user interface, you would want the settings retained for each of the forms in your application.

One way to define settings for each of your forms is to define a unique set of settings for each form. You could then use the techniques defined above. But that would be a significant amount of work, especially if your application has a large number of forms.

A better approach is to create your own MySettings object and use the SettingsKey property to define a key for each form. To minimize the amount of repeated code required for this, add the code to a base form class as defined in my article, “Give Your Forms a Base” (CoDe Magazine March/April 2004).

To define multiple sets of settings, begin by creating your own MySettings object. Insert a property into your base form class that will create and retain your specialized MySettings object:

Private _settings As My.MySettings
Private ReadOnly Property Settings() As _
    System.Configuration.ApplicationSettingsBase
  Get
    If _settings Is Nothing Then
      _settings = New My.MySettings
    End If
    Return _settings
  End Get
End Property

This code declares a private member variable to retain your custom MySettings object. It defines a private read-only property that will initially create an instance of your custom setting. It then returns the custom setting for use within the base form class.

&


Article Pages: < Previous - 1  2  3 - Next Page: 'Retaining Multiple Sets of User Settings (con't)' >>

Page 1: Retaining Multiple Sets of User Settings
Page 2: Retaining Multiple Sets of User Settings (con't)
Page 3: Retaining Multiple Sets of User Settings (con't)

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
3.7 out of 5

17 people have rated this article.

Hacker Halted

      Sharepoint TechCon

 

Hacker Halted