Click here to Skip to main content
15,885,915 members
Articles / Desktop Programming / Windows Forms

User Settings Applied

Rate me:
Please Sign up or sign in to vote.
4.95/5 (136 votes)
5 Sep 2013CPOL10 min read 542.1K   7.5K   489  
Simplifying the .NET User Configuration for Windows Forms and WPF
// -- FILE ------------------------------------------------------------------
// name       : ListViewSettingsWindow.xaml.cs
// created    : Jani Giannoudis - 2008.05.09
// language   : c#
// environment: .NET 3.0
// --------------------------------------------------------------------------

using System.Globalization;
using System.Text;
using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Itenso.Configuration;

namespace Itenso.Solutions.Community.ConfigurationWindowsDemo
{

	// ------------------------------------------------------------------------
	public partial class ListViewSettingsWindow
	{

		// ----------------------------------------------------------------------
		public ListViewSettingsWindow()
		{
			InitializeComponent();
			WindowSettings.SaveOnClose = false; // disable auto-save
			WindowSettings.Settings.Add( new ListViewSetting( CustomerListView ) );
		} // ListViewSettingsWindow

		// ----------------------------------------------------------------------
		public ObservableCollection<Customer> Customers
		{
			get { return customers ?? ( customers = LoadCustomers() ); }
		} // Customers

		// ----------------------------------------------------------------------
		protected override void OnClosing( CancelEventArgs e )
		{
			base.OnClosing( e );

			if ( DialogResult == false )
			{
				return; // is canceling
			}

			if ( !WindowSettings.Settings.HasChanges )
			{
				return; // nothing to do
			}

			StringBuilder sb = new StringBuilder();
			foreach ( ISetting setting in WindowSettings.Settings )
			{
				if ( !setting.HasChanged )
				{
					continue;
				}

				sb.Append( " - " );
				sb.Append( setting.ToString() );
				sb.Append( "\n" );
			}

			MessageBoxResult result = MessageBox.Show( "Save changes?\n\n" + sb, Title, MessageBoxButton.YesNoCancel );
			switch ( result )
			{
				case MessageBoxResult.Yes:
					WindowSettings.Save();
					break;
				case MessageBoxResult.No:
					break;
				case MessageBoxResult.Cancel:
					DialogResult = null;
					e.Cancel = true;
					break;
			}
		} // OnClosing

		// ----------------------------------------------------------------------
		private static ObservableCollection<Customer> LoadCustomers()
		{
			ObservableCollection<Customer> customers = new ObservableCollection<Customer>();

			for ( int i = 0; i < 100; i++ )
			{
				string userId = ( i + 1 ).ToString( CultureInfo.InvariantCulture );
				Customer customer = new Customer(
					"FisrtName" + userId,
					"LastName" + userId,
					"Street" + userId,
					"City" + userId,
					"ZipCode" + userId );

				customers.Add( customer );
			}

			return customers;
		} // LoadCustomers

		// ----------------------------------------------------------------------
		private void ButtonCancel( object sender, RoutedEventArgs e )
		{
			DialogResult = false;
		} // ButtonCancel

		// ----------------------------------------------------------------------
		private void ButtonOk( object sender, RoutedEventArgs e )
		{
			DialogResult = true;
		} // ButtonOk

		// ----------------------------------------------------------------------
		// members
		private ObservableCollection<Customer> customers;

	} // class ListViewSettingsWindow

} // namespace Itenso.Solutions.Community.ConfigurationWindowsDemo
// -- EOF -------------------------------------------------------------------

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions