Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Saving and Restoring Application Settings

, 15 Mar 2003
The Savior class makes it simple to save and restore application settings using the registry or a binary file.
savior.zip
Savior.exe
saviorsource.zip
App.ico
Savior.csproj.user
using System;
using System.Windows.Forms;
using System.Collections;
using System.Runtime.InteropServices;

namespace SaveListView
{
	/// <summary>
	/// This class was derived from  a class written by dfontanesi at The Code Project:
	/// 
	/// http://www.codeproject.com/useritems/savelistview.asp?target=listviewsettings
	/// 
	/// --------------------------jnh 1/10/03--------------------
	/// I simplified the class considerably since it only needs to keep track of
	/// the column order. The Interop stuff is copied exactly, but the rest of the
	/// class was considerably modified and simplified. I no longer need to instantiate
	/// a copy of this class since it just contains two static methods to get and set
	/// the column order. It is a lot cleaner and clearer (to my mind).
	/// --------------------------jnh 1/11/03--------------------
	/// 
	/// This class is used to get and set the order settings
	/// for a ListView control.  Unfortunately, there is no easy way to obtain the
	/// order so a Windows message must be sent to the control.  This class
	/// encapsulates that functionality 
	/// </summary>
	public class ListViewSettings
	{		
		[DllImport("user32.dll")]
		static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, ref
			LV_COLUMN lParam);

		[StructLayoutAttribute(LayoutKind.Sequential)]
		struct LV_COLUMN
		{
			public UInt32 mask;
			public Int32 fmt;
			public Int32 cx;
			public String pszText;
			public Int32 cchTextMax;
			public Int32 iSubItem;
			public Int32 iImage;
			public Int32 iOrder;
		}  

		const Int32 LVM_FIRST = 0x1000;
		const Int32 LVM_GETCOLUMN = LVM_FIRST + 95;
		const Int32 LVM_SETCOLUMN = LVM_FIRST + 96;
		const Int32 LVCF_ORDER = 0x0020;

		/// <summary>
		/// Return the order of presentation of the columns in a ListView control
		/// </summary>
		/// <param name="listView">The ListView control to be queried</param>
		/// <returns>An array of integers giving the order for each column</returns>
		public static int[] GetColumnOrders( ListView listView )
		{
			try
			{
				int[] ColOrders = new int[listView.Columns.Count];
				foreach( ColumnHeader column in listView.Columns )
				{
					LV_COLUMN pcol = new LV_COLUMN();
					pcol.mask = LVCF_ORDER;
					bool ret = SendMessage(listView.Handle, LVM_GETCOLUMN, column.Index, ref pcol);
					ColOrders[column.Index]=pcol.iOrder;
				}
				return ColOrders;
			}
			catch {
				return null;
			}
		}

		/// <summary>
		/// Set the order of presentation of the columns in a ListView control
		/// </summary>
		/// <param name="listView">The ListView to be modified</param>
		/// <param name="ColOrders">An array of integers specifying the column order, no vlaidity check is done</param>
		public static void SetColumnOrders( ListView listView, int[] ColOrders )
		{
			try
			{
				listView.Hide();
					foreach( ColumnHeader column in listView.Columns )
					{
						int i=column.Index;
						LV_COLUMN pcol = new LV_COLUMN();
						pcol.mask = LVCF_ORDER;
						pcol.iOrder = ColOrders[i];
						bool ret = SendMessage(listView.Handle, LVM_SETCOLUMN, column.Index, ref pcol);
					}
				listView.Show();
			}
			catch {}
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Jim Hollenhorst
Researcher
United States United States
Ultrapico Website: http://www.ultrapico.com
 
Download Expresso 3.0, the latest version of the award-winning regular expression development tool.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150129.1 | Last Updated 16 Mar 2003
Article Copyright 2003 by Jim Hollenhorst
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid