What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 15394 |  Welcome, Guest!   Register  Login
Home > Articles > WPF > Application Settings in WPF

Application Settings in WPF

3 vote(s)
Rating: 4.33 out of 5
Article posted by Naimishforu on 2/8/2011 | Views: 12631 | Category: WPF | Level: Beginner | Points: 250 red flag


You can create and access values which can stay from application session to application session. This values are called as Settings in WPF.

Download


 Download source code for Application Settings in WPF


Introduction


The Settings can have any type of information which we require to store somewhere and later on we can use it.  It's similar to Session Technics in Web Development.  We can store some information in settings and we can also change the same, even at run-time.

Consider Settings as some centralized storage location.

Basically, Settings have 4 Properties:

  1. Name   (Name of the Setting)
  2. Type   (Data Type of the Setting)
  3. Value   (The information which will get store in Setting)
  4. Scope  (User or Application)

The bit confusing thing over here is a property called Scope.  Scope has two option as User or Application.  I'll try to describe the same in similar way,

User - The Session is restricted to the User 
Application -  The Session is restricted to the Application

OK, good enough.  But is there any difference between this too?? Can be asked somewhere in interviews!!! Huh!!

Yes, User settings are read/write while Application Settings are read-only.  (aah!! now what is this!!!)

User Settings can be access or modify while your application is running (run-time).  When you modify the User Settings, it will get saved by the application and immediate affect will be taken place.

Application Settings are read-only means that you can't change the value of Application Settings at run-time.  You will to change the same at design-time.

Woh Woh!! enough of theory, now let's go and do some hands-on guys!!

So, How to create Settings (Application or User):

Note: I am using Visual Studio 2010 and VB.NET as Language

  1. Go to the Solution Explorer
  2. Double-click My Project
  3. Go to the Settings tab



Note : You will not find the same value appears here as in your Settings tab.  ( I have updated the Settings file for me )

4.  Change the settings as been seen in above picture.

5.  Go to your XAML file and paste the below code :

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox Margin="15,15,0,0" Name="listBox1" Height="78"
                 HorizontalAlignment="Left" VerticalAlignment="Top" Width="107">
            <ListBoxItem>New York</ListBoxItem>
            <ListBoxItem>Sydney</ListBoxItem>
            <ListBoxItem>London</ListBoxItem>
            <ListBoxItem>Other</ListBoxItem>
        </ListBox>
        <Button Margin="15,106,110,130" Name="button1">Change User City</Button>
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="136,15,0,0" Name="TextBlock1" Text="TextBlock" VerticalAlignment="Top" />
    </Grid>
</Window>    

7.  Go to your source-code file and paste the below code :

VB.NET Code :
Class MainWindow
    Dim astring As String
    Public Sub New()

        InitializeComponent()

        Me.Title = My.Settings.UserName
        TextBlock1.Text = "Previously Selected City " + My.Settings.UserCity

    End Sub

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles button1.Click

        If Not listBox1.SelectedItem Is Nothing Then
            TextBlock1.Text = "Previously Selected City " + astring
            astring = CType(listBox1.SelectedItem, ListBoxItem).Content.ToString
            My.Settings.UserCity = astring
            My.Settings.Save()
        End If
    End Sub
End Class
C#.NET Code : 

class MainWindow
{
	string astring;

	public MainWindow()
	{
		InitializeComponent();

		this.Title = My.Settings.UserName;
		TextBlock1.Text = "Previously Selected City " + My.Settings.UserCity;

	}


	private void  // ERROR: Handles clauses are not supported in C#
button1_Click(System.Object sender, System.Windows.RoutedEventArgs e)
	{
		if ((listBox1.SelectedItem != null)) {
			TextBlock1.Text = "Previously Selected City " + astring;
			astring = ((ListBoxItem)listBox1.SelectedItem).Content.ToString;
			My.Settings.UserCity = astring;
			My.Settings.Save();
		}
	}
}


8.  Press F5 to run the application :



9.  Now click on New York and Press Change User City button:



10.  Now close the application.

11.  Again press F5 and run the application.



Thanks and Have Fun!!!!!

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:5 year(s)
Home page:http://naimishpandya.blogspot.in/
Member since:Saturday, January 22, 2011
Level:Bronze
Status: [Member] [MVP]
Biography:
 Responses
Posted by: Sutotpal | Posted on: 08 Feb 2011 04:33:48 AM | Points: 25

hi...
It's a nice article for beginners...

Thanks

Posted by: Naimishforu | Posted on: 08 Feb 2011 04:44:09 AM | Points: 25

Thanks Sutotpal :)

Posted by: Karthikanbarasan | Posted on: 11 Feb 2011 12:09:17 AM | Points: 25

I dnt see any images here!!!

Posted by: Naimishforu | Posted on: 11 Feb 2011 12:19:44 AM | Points: 25

Yes Karthik, there is some problem with where I have uploaded images.

Will update it soon.

Thanks

Posted by: Karthikanbarasan | Posted on: 11 Feb 2011 12:35:36 AM | Points: 25

oh ok

Posted by: Naimishforu | Posted on: 11 Feb 2011 01:20:23 AM | Points: 25

Hi,

I hope it's fine now :)

Thanks

Posted by: Karthikanbarasan | Posted on: 11 Feb 2011 01:32:07 AM | Points: 25

Yeah i can see the images!!!

>> Write Response - Respond to this post and get points
Related Posts

We will look into three main types of in WPF.

WPF added lot of flexibility and usability to the client application development. There are lots of features in WPF for enhancing the usability aspects of a client application. One of the interesting and very useful features is the WPF adorner. In this article we will discuss briefly about Adorner and how to create a simple adorner.

The article will guide through the basics of WPF programing with in-depth knowledge about the architecture and the working principles of WPF programs. The article finally creates a sample "Hello World" application to step you into new foundation. Even though I have started from the beginning of WPF, I marked it as Intermediate article, as I will also go indepth of all the techniques while discussing it. This is the first part of the WPF turorial. Stay tune for others.

In this article, we will explore the concept of MultiBinding and IMultiValueConverter with the help of simple example.

Here I have showed a simple trick to define base class for WPF Window and UserControl.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 5:10:44 AM