Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

Persist ListView settings with serialization

Rate me:
Please Sign up or sign in to vote.
4.73/5 (29 votes)
8 Dec 20023 min read 114.9K   928   49  
Serialization is a powerful feature of .NET. Here I use it to add functionality to the ListView control.
using System;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Collections;
using System.Runtime.InteropServices;

namespace SaveListView
{
	/// <summary>
	/// This class is used to save and restore the column width and 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 as well as the serialization/deserialization
	/// of the settings.
	/// </summary>
	[Serializable]
	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;

		[XmlElement("ListViewColumns", typeof(ListViewColumn))]
		public ArrayList listViewCols = new ArrayList();

		public ListViewSettings( ListView listView )
		{
			try
			{
				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);
					listViewCols.Add( new ListViewColumn( column.Text, column.Width, pcol.iOrder ));
				}
			}
			catch {}
		}

		public void RestoreFormat( ListView listView )
		{
			try
			{
				listView.Hide();
				for( int i=0; i<listViewCols.Count; i++ )
				{
					foreach( ColumnHeader column in listView.Columns )
					{
						if( column.Text == ((ListViewColumn)listViewCols[i]).header )
						{
							LV_COLUMN pcol = new LV_COLUMN();
							pcol.mask = LVCF_ORDER;
							pcol.iOrder = ((ListViewColumn)listViewCols[i]).order;
							bool ret = SendMessage(listView.Handle, LVM_SETCOLUMN, column.Index, ref pcol);
							column.Width = ((ListViewColumn)listViewCols[i]).width;
							break;
						}
					}
				}
				listView.Show();
			}
			catch {}
		}
	}

	[Serializable]
	public class ListViewColumn
	{
		public string header;
		public int width;
		public int order;

		public ListViewColumn( string colHeader, int colWidth, int colOrder )
		{
			header = colHeader;
			width = colWidth;
			order = colOrder;
		}
	}
}

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


Written By
Web Developer
United States United States
I'm a software engineer and consultant working in San Diego, California. I began using .NET during the early alpha releases since I worked for a Microsoft subsidiary then, and now I've focused my career on .NET technologies.

There are a lot of .NET sites out there now, but The Code Project is still top of the heap.

Comments and Discussions