Click here to Skip to main content
15,896,342 members
Articles / Programming Languages / C#

ListView Personalization

Rate me:
Please Sign up or sign in to vote.
4.20/5 (6 votes)
30 Jul 2007CPOL1 min read 49.2K   1.8K   43  
An article on the personalization and persistance of ListView settings
using System;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.ComponentModel;
using System.Windows.Forms;

//using Utiliti

namespace Personalisation
{
	/// <summary>
	/// Possible states for the windows show / hide buttons
	/// </summary>
	internal enum ButtonEnabledState
	{
		/// <summary>
		/// The show button should be enabled
		/// </summary>
		Show,
		/// <summary>
		/// The hide button should be enabled
		/// </summary>
		Hide,
		/// <summary>
		/// No buttons should be enabled
		/// </summary>
		None
	}

	/// <summary>
	/// Form to display and alter the settings for ListView columns.
	/// </summary>
	public class ColumnSettingsUI : Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button moveUpButton;
		private System.Windows.Forms.Button moveDownButton;
		private System.Windows.Forms.Button showButton;
		private System.Windows.Forms.Button hideButton;
		private System.Windows.Forms.Button cancelButton;
		private System.Windows.Forms.Button okButton;
		private System.Windows.Forms.TextBox columnWidthTextBox;
		private System.Windows.Forms.TextBox preferedCaptionTextBox;
		private System.Windows.Forms.ListView columnsListView;
		private System.Windows.Forms.Label infoLabel;
		private System.Windows.Forms.Label columnsLabel;
		private System.Windows.Forms.GroupBox separatorGroupBox;
		private System.Windows.Forms.Label widthLabel;
		private System.Windows.Forms.Label preferredCaptionLabel;
		private bool suppressUpdates;

		private ColumnSettings columnSettings;

		/// <summary>
		/// Initializes a new instance of the <see cref="ColumnSettings"/> class.
		/// </summary>
		/// <param name="columnDetails">The current ListView column details.</param>
		public ColumnSettingsUI(ColumnDetails[] columnDetails)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			columnSettings = new ColumnSettings(columnDetails, columnsListView);
			columnSettings.AddColumns();
		}

		/// <summary>
		/// Gets or sets the column details.
		/// </summary>
		/// <value>The column details.</value>
		public ColumnDetails[] ColumnDetails
		{
			get{return columnSettings.ColumnDetails;}
			set{columnSettings.ColumnDetails = value;}
		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ColumnSettingsUI));
			this.infoLabel = new System.Windows.Forms.Label();
			this.columnsListView = new System.Windows.Forms.ListView();
			this.columnsLabel = new System.Windows.Forms.Label();
			this.moveUpButton = new System.Windows.Forms.Button();
			this.moveDownButton = new System.Windows.Forms.Button();
			this.showButton = new System.Windows.Forms.Button();
			this.hideButton = new System.Windows.Forms.Button();
			this.separatorGroupBox = new System.Windows.Forms.GroupBox();
			this.widthLabel = new System.Windows.Forms.Label();
			this.columnWidthTextBox = new System.Windows.Forms.TextBox();
			this.cancelButton = new System.Windows.Forms.Button();
			this.okButton = new System.Windows.Forms.Button();
			this.preferredCaptionLabel = new System.Windows.Forms.Label();
			this.preferedCaptionTextBox = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// infoLabel
			// 
			this.infoLabel.AccessibleDescription = resources.GetString("infoLabel.AccessibleDescription");
			this.infoLabel.AccessibleName = resources.GetString("infoLabel.AccessibleName");
			this.infoLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("infoLabel.Anchor")));
			this.infoLabel.AutoSize = ((bool)(resources.GetObject("infoLabel.AutoSize")));
			this.infoLabel.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("infoLabel.Dock")));
			this.infoLabel.Enabled = ((bool)(resources.GetObject("infoLabel.Enabled")));
			this.infoLabel.Font = ((System.Drawing.Font)(resources.GetObject("infoLabel.Font")));
			this.infoLabel.Image = ((System.Drawing.Image)(resources.GetObject("infoLabel.Image")));
			this.infoLabel.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("infoLabel.ImageAlign")));
			this.infoLabel.ImageIndex = ((int)(resources.GetObject("infoLabel.ImageIndex")));
			this.infoLabel.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("infoLabel.ImeMode")));
			this.infoLabel.Location = ((System.Drawing.Point)(resources.GetObject("infoLabel.Location")));
			this.infoLabel.Name = "infoLabel";
			this.infoLabel.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("infoLabel.RightToLeft")));
			this.infoLabel.Size = ((System.Drawing.Size)(resources.GetObject("infoLabel.Size")));
			this.infoLabel.TabIndex = ((int)(resources.GetObject("infoLabel.TabIndex")));
			this.infoLabel.Text = resources.GetString("infoLabel.Text");
			this.infoLabel.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("infoLabel.TextAlign")));
			this.infoLabel.Visible = ((bool)(resources.GetObject("infoLabel.Visible")));
			// 
			// columnsListView
			// 
			this.columnsListView.AccessibleDescription = resources.GetString("columnsListView.AccessibleDescription");
			this.columnsListView.AccessibleName = resources.GetString("columnsListView.AccessibleName");
			this.columnsListView.Alignment = ((System.Windows.Forms.ListViewAlignment)(resources.GetObject("columnsListView.Alignment")));
			this.columnsListView.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("columnsListView.Anchor")));
			this.columnsListView.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("columnsListView.BackgroundImage")));
			this.columnsListView.CheckBoxes = true;
			this.columnsListView.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("columnsListView.Dock")));
			this.columnsListView.Enabled = ((bool)(resources.GetObject("columnsListView.Enabled")));
			this.columnsListView.Font = ((System.Drawing.Font)(resources.GetObject("columnsListView.Font")));
			this.columnsListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
			this.columnsListView.HideSelection = false;
			this.columnsListView.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("columnsListView.ImeMode")));
			this.columnsListView.LabelWrap = ((bool)(resources.GetObject("columnsListView.LabelWrap")));
			this.columnsListView.Location = ((System.Drawing.Point)(resources.GetObject("columnsListView.Location")));
			this.columnsListView.MultiSelect = false;
			this.columnsListView.Name = "columnsListView";
			this.columnsListView.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("columnsListView.RightToLeft")));
			this.columnsListView.Size = ((System.Drawing.Size)(resources.GetObject("columnsListView.Size")));
			this.columnsListView.TabIndex = ((int)(resources.GetObject("columnsListView.TabIndex")));
			this.columnsListView.Text = resources.GetString("columnsListView.Text");
			this.columnsListView.View = System.Windows.Forms.View.List;
			this.columnsListView.Visible = ((bool)(resources.GetObject("columnsListView.Visible")));
			this.columnsListView.SelectedIndexChanged += new System.EventHandler(this.OnSelectedIndexChanged);
			this.columnsListView.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.OnItemChecked);
			// 
			// columnsLabel
			// 
			this.columnsLabel.AccessibleDescription = resources.GetString("columnsLabel.AccessibleDescription");
			this.columnsLabel.AccessibleName = resources.GetString("columnsLabel.AccessibleName");
			this.columnsLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("columnsLabel.Anchor")));
			this.columnsLabel.AutoSize = ((bool)(resources.GetObject("columnsLabel.AutoSize")));
			this.columnsLabel.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("columnsLabel.Dock")));
			this.columnsLabel.Enabled = ((bool)(resources.GetObject("columnsLabel.Enabled")));
			this.columnsLabel.Font = ((System.Drawing.Font)(resources.GetObject("columnsLabel.Font")));
			this.columnsLabel.Image = ((System.Drawing.Image)(resources.GetObject("columnsLabel.Image")));
			this.columnsLabel.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("columnsLabel.ImageAlign")));
			this.columnsLabel.ImageIndex = ((int)(resources.GetObject("columnsLabel.ImageIndex")));
			this.columnsLabel.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("columnsLabel.ImeMode")));
			this.columnsLabel.Location = ((System.Drawing.Point)(resources.GetObject("columnsLabel.Location")));
			this.columnsLabel.Name = "columnsLabel";
			this.columnsLabel.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("columnsLabel.RightToLeft")));
			this.columnsLabel.Size = ((System.Drawing.Size)(resources.GetObject("columnsLabel.Size")));
			this.columnsLabel.TabIndex = ((int)(resources.GetObject("columnsLabel.TabIndex")));
			this.columnsLabel.Text = resources.GetString("columnsLabel.Text");
			this.columnsLabel.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("columnsLabel.TextAlign")));
			this.columnsLabel.Visible = ((bool)(resources.GetObject("columnsLabel.Visible")));
			// 
			// moveUpButton
			// 
			this.moveUpButton.AccessibleDescription = resources.GetString("moveUpButton.AccessibleDescription");
			this.moveUpButton.AccessibleName = resources.GetString("moveUpButton.AccessibleName");
			this.moveUpButton.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("moveUpButton.Anchor")));
			this.moveUpButton.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("moveUpButton.BackgroundImage")));
			this.moveUpButton.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("moveUpButton.Dock")));
			this.moveUpButton.Enabled = ((bool)(resources.GetObject("moveUpButton.Enabled")));
			this.moveUpButton.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("moveUpButton.FlatStyle")));
			this.moveUpButton.Font = ((System.Drawing.Font)(resources.GetObject("moveUpButton.Font")));
			this.moveUpButton.Image = ((System.Drawing.Image)(resources.GetObject("moveUpButton.Image")));
			this.moveUpButton.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("moveUpButton.ImageAlign")));
			this.moveUpButton.ImageIndex = ((int)(resources.GetObject("moveUpButton.ImageIndex")));
			this.moveUpButton.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("moveUpButton.ImeMode")));
			this.moveUpButton.Location = ((System.Drawing.Point)(resources.GetObject("moveUpButton.Location")));
			this.moveUpButton.Name = "moveUpButton";
			this.moveUpButton.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("moveUpButton.RightToLeft")));
			this.moveUpButton.Size = ((System.Drawing.Size)(resources.GetObject("moveUpButton.Size")));
			this.moveUpButton.TabIndex = ((int)(resources.GetObject("moveUpButton.TabIndex")));
			this.moveUpButton.Text = resources.GetString("moveUpButton.Text");
			this.moveUpButton.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("moveUpButton.TextAlign")));
			this.moveUpButton.Visible = ((bool)(resources.GetObject("moveUpButton.Visible")));
			this.moveUpButton.Click += new System.EventHandler(this.OnMoveUp);
			// 
			// moveDownButton
			// 
			this.moveDownButton.AccessibleDescription = resources.GetString("moveDownButton.AccessibleDescription");
			this.moveDownButton.AccessibleName = resources.GetString("moveDownButton.AccessibleName");
			this.moveDownButton.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("moveDownButton.Anchor")));
			this.moveDownButton.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("moveDownButton.BackgroundImage")));
			this.moveDownButton.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("moveDownButton.Dock")));
			this.moveDownButton.Enabled = ((bool)(resources.GetObject("moveDownButton.Enabled")));
			this.moveDownButton.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("moveDownButton.FlatStyle")));
			this.moveDownButton.Font = ((System.Drawing.Font)(resources.GetObject("moveDownButton.Font")));
			this.moveDownButton.Image = ((System.Drawing.Image)(resources.GetObject("moveDownButton.Image")));
			this.moveDownButton.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("moveDownButton.ImageAlign")));
			this.moveDownButton.ImageIndex = ((int)(resources.GetObject("moveDownButton.ImageIndex")));
			this.moveDownButton.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("moveDownButton.ImeMode")));
			this.moveDownButton.Location = ((System.Drawing.Point)(resources.GetObject("moveDownButton.Location")));
			this.moveDownButton.Name = "moveDownButton";
			this.moveDownButton.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("moveDownButton.RightToLeft")));
			this.moveDownButton.Size = ((System.Drawing.Size)(resources.GetObject("moveDownButton.Size")));
			this.moveDownButton.TabIndex = ((int)(resources.GetObject("moveDownButton.TabIndex")));
			this.moveDownButton.Text = resources.GetString("moveDownButton.Text");
			this.moveDownButton.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("moveDownButton.TextAlign")));
			this.moveDownButton.Visible = ((bool)(resources.GetObject("moveDownButton.Visible")));
			this.moveDownButton.Click += new System.EventHandler(this.OnMoveDown);
			// 
			// showButton
			// 
			this.showButton.AccessibleDescription = resources.GetString("showButton.AccessibleDescription");
			this.showButton.AccessibleName = resources.GetString("showButton.AccessibleName");
			this.showButton.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("showButton.Anchor")));
			this.showButton.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("showButton.BackgroundImage")));
			this.showButton.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("showButton.Dock")));
			this.showButton.Enabled = ((bool)(resources.GetObject("showButton.Enabled")));
			this.showButton.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("showButton.FlatStyle")));
			this.showButton.Font = ((System.Drawing.Font)(resources.GetObject("showButton.Font")));
			this.showButton.Image = ((System.Drawing.Image)(resources.GetObject("showButton.Image")));
			this.showButton.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("showButton.ImageAlign")));
			this.showButton.ImageIndex = ((int)(resources.GetObject("showButton.ImageIndex")));
			this.showButton.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("showButton.ImeMode")));
			this.showButton.Location = ((System.Drawing.Point)(resources.GetObject("showButton.Location")));
			this.showButton.Name = "showButton";
			this.showButton.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("showButton.RightToLeft")));
			this.showButton.Size = ((System.Drawing.Size)(resources.GetObject("showButton.Size")));
			this.showButton.TabIndex = ((int)(resources.GetObject("showButton.TabIndex")));
			this.showButton.Text = resources.GetString("showButton.Text");
			this.showButton.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("showButton.TextAlign")));
			this.showButton.Visible = ((bool)(resources.GetObject("showButton.Visible")));
			this.showButton.Click += new System.EventHandler(this.ShowButtonClick);
			// 
			// hideButton
			// 
			this.hideButton.AccessibleDescription = resources.GetString("hideButton.AccessibleDescription");
			this.hideButton.AccessibleName = resources.GetString("hideButton.AccessibleName");
			this.hideButton.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("hideButton.Anchor")));
			this.hideButton.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("hideButton.BackgroundImage")));
			this.hideButton.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("hideButton.Dock")));
			this.hideButton.Enabled = ((bool)(resources.GetObject("hideButton.Enabled")));
			this.hideButton.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("hideButton.FlatStyle")));
			this.hideButton.Font = ((System.Drawing.Font)(resources.GetObject("hideButton.Font")));
			this.hideButton.Image = ((System.Drawing.Image)(resources.GetObject("hideButton.Image")));
			this.hideButton.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("hideButton.ImageAlign")));
			this.hideButton.ImageIndex = ((int)(resources.GetObject("hideButton.ImageIndex")));
			this.hideButton.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("hideButton.ImeMode")));
			this.hideButton.Location = ((System.Drawing.Point)(resources.GetObject("hideButton.Location")));
			this.hideButton.Name = "hideButton";
			this.hideButton.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("hideButton.RightToLeft")));
			this.hideButton.Size = ((System.Drawing.Size)(resources.GetObject("hideButton.Size")));
			this.hideButton.TabIndex = ((int)(resources.GetObject("hideButton.TabIndex")));
			this.hideButton.Text = resources.GetString("hideButton.Text");
			this.hideButton.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("hideButton.TextAlign")));
			this.hideButton.Visible = ((bool)(resources.GetObject("hideButton.Visible")));
			this.hideButton.Click += new System.EventHandler(this.HideButtonClick);
			// 
			// separatorGroupBox
			// 
			this.separatorGroupBox.AccessibleDescription = resources.GetString("separatorGroupBox.AccessibleDescription");
			this.separatorGroupBox.AccessibleName = resources.GetString("separatorGroupBox.AccessibleName");
			this.separatorGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("separatorGroupBox.Anchor")));
			this.separatorGroupBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("separatorGroupBox.BackgroundImage")));
			this.separatorGroupBox.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("separatorGroupBox.Dock")));
			this.separatorGroupBox.Enabled = ((bool)(resources.GetObject("separatorGroupBox.Enabled")));
			this.separatorGroupBox.Font = ((System.Drawing.Font)(resources.GetObject("separatorGroupBox.Font")));
			this.separatorGroupBox.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("separatorGroupBox.ImeMode")));
			this.separatorGroupBox.Location = ((System.Drawing.Point)(resources.GetObject("separatorGroupBox.Location")));
			this.separatorGroupBox.Name = "separatorGroupBox";
			this.separatorGroupBox.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("separatorGroupBox.RightToLeft")));
			this.separatorGroupBox.Size = ((System.Drawing.Size)(resources.GetObject("separatorGroupBox.Size")));
			this.separatorGroupBox.TabIndex = ((int)(resources.GetObject("separatorGroupBox.TabIndex")));
			this.separatorGroupBox.TabStop = false;
			this.separatorGroupBox.Text = resources.GetString("separatorGroupBox.Text");
			this.separatorGroupBox.Visible = ((bool)(resources.GetObject("separatorGroupBox.Visible")));
			// 
			// widthLabel
			// 
			this.widthLabel.AccessibleDescription = resources.GetString("widthLabel.AccessibleDescription");
			this.widthLabel.AccessibleName = resources.GetString("widthLabel.AccessibleName");
			this.widthLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("widthLabel.Anchor")));
			this.widthLabel.AutoSize = ((bool)(resources.GetObject("widthLabel.AutoSize")));
			this.widthLabel.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("widthLabel.Dock")));
			this.widthLabel.Enabled = ((bool)(resources.GetObject("widthLabel.Enabled")));
			this.widthLabel.Font = ((System.Drawing.Font)(resources.GetObject("widthLabel.Font")));
			this.widthLabel.Image = ((System.Drawing.Image)(resources.GetObject("widthLabel.Image")));
			this.widthLabel.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("widthLabel.ImageAlign")));
			this.widthLabel.ImageIndex = ((int)(resources.GetObject("widthLabel.ImageIndex")));
			this.widthLabel.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("widthLabel.ImeMode")));
			this.widthLabel.Location = ((System.Drawing.Point)(resources.GetObject("widthLabel.Location")));
			this.widthLabel.Name = "widthLabel";
			this.widthLabel.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("widthLabel.RightToLeft")));
			this.widthLabel.Size = ((System.Drawing.Size)(resources.GetObject("widthLabel.Size")));
			this.widthLabel.TabIndex = ((int)(resources.GetObject("widthLabel.TabIndex")));
			this.widthLabel.Text = resources.GetString("widthLabel.Text");
			this.widthLabel.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("widthLabel.TextAlign")));
			this.widthLabel.Visible = ((bool)(resources.GetObject("widthLabel.Visible")));
			// 
			// columnWidthTextBox
			// 
			this.columnWidthTextBox.AccessibleDescription = resources.GetString("columnWidthTextBox.AccessibleDescription");
			this.columnWidthTextBox.AccessibleName = resources.GetString("columnWidthTextBox.AccessibleName");
			this.columnWidthTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("columnWidthTextBox.Anchor")));
			this.columnWidthTextBox.AutoSize = ((bool)(resources.GetObject("columnWidthTextBox.AutoSize")));
			this.columnWidthTextBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("columnWidthTextBox.BackgroundImage")));
			this.columnWidthTextBox.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("columnWidthTextBox.Dock")));
			this.columnWidthTextBox.Enabled = ((bool)(resources.GetObject("columnWidthTextBox.Enabled")));
			this.columnWidthTextBox.Font = ((System.Drawing.Font)(resources.GetObject("columnWidthTextBox.Font")));
			this.columnWidthTextBox.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("columnWidthTextBox.ImeMode")));
			this.columnWidthTextBox.Location = ((System.Drawing.Point)(resources.GetObject("columnWidthTextBox.Location")));
			this.columnWidthTextBox.MaxLength = ((int)(resources.GetObject("columnWidthTextBox.MaxLength")));
			this.columnWidthTextBox.Multiline = ((bool)(resources.GetObject("columnWidthTextBox.Multiline")));
			this.columnWidthTextBox.Name = "columnWidthTextBox";
			this.columnWidthTextBox.PasswordChar = ((char)(resources.GetObject("columnWidthTextBox.PasswordChar")));
			this.columnWidthTextBox.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("columnWidthTextBox.RightToLeft")));
			this.columnWidthTextBox.ScrollBars = ((System.Windows.Forms.ScrollBars)(resources.GetObject("columnWidthTextBox.ScrollBars")));
			this.columnWidthTextBox.Size = ((System.Drawing.Size)(resources.GetObject("columnWidthTextBox.Size")));
			this.columnWidthTextBox.TabIndex = ((int)(resources.GetObject("columnWidthTextBox.TabIndex")));
			this.columnWidthTextBox.Text = resources.GetString("columnWidthTextBox.Text");
			this.columnWidthTextBox.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnWidthTextBox.TextAlign")));
			this.columnWidthTextBox.Visible = ((bool)(resources.GetObject("columnWidthTextBox.Visible")));
			this.columnWidthTextBox.WordWrap = ((bool)(resources.GetObject("columnWidthTextBox.WordWrap")));
			this.columnWidthTextBox.TextChanged += new System.EventHandler(this.OnWidthChanged);
			// 
			// cancelButton
			// 
			this.cancelButton.AccessibleDescription = resources.GetString("cancelButton.AccessibleDescription");
			this.cancelButton.AccessibleName = resources.GetString("cancelButton.AccessibleName");
			this.cancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("cancelButton.Anchor")));
			this.cancelButton.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("cancelButton.BackgroundImage")));
			this.cancelButton.CausesValidation = false;
			this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cancelButton.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("cancelButton.Dock")));
			this.cancelButton.Enabled = ((bool)(resources.GetObject("cancelButton.Enabled")));
			this.cancelButton.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("cancelButton.FlatStyle")));
			this.cancelButton.Font = ((System.Drawing.Font)(resources.GetObject("cancelButton.Font")));
			this.cancelButton.Image = ((System.Drawing.Image)(resources.GetObject("cancelButton.Image")));
			this.cancelButton.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("cancelButton.ImageAlign")));
			this.cancelButton.ImageIndex = ((int)(resources.GetObject("cancelButton.ImageIndex")));
			this.cancelButton.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("cancelButton.ImeMode")));
			this.cancelButton.Location = ((System.Drawing.Point)(resources.GetObject("cancelButton.Location")));
			this.cancelButton.Name = "cancelButton";
			this.cancelButton.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("cancelButton.RightToLeft")));
			this.cancelButton.Size = ((System.Drawing.Size)(resources.GetObject("cancelButton.Size")));
			this.cancelButton.TabIndex = ((int)(resources.GetObject("cancelButton.TabIndex")));
			this.cancelButton.Text = resources.GetString("cancelButton.Text");
			this.cancelButton.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("cancelButton.TextAlign")));
			this.cancelButton.Visible = ((bool)(resources.GetObject("cancelButton.Visible")));
			// 
			// okButton
			// 
			this.okButton.AccessibleDescription = resources.GetString("okButton.AccessibleDescription");
			this.okButton.AccessibleName = resources.GetString("okButton.AccessibleName");
			this.okButton.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("okButton.Anchor")));
			this.okButton.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("okButton.BackgroundImage")));
			this.okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.okButton.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("okButton.Dock")));
			this.okButton.Enabled = ((bool)(resources.GetObject("okButton.Enabled")));
			this.okButton.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("okButton.FlatStyle")));
			this.okButton.Font = ((System.Drawing.Font)(resources.GetObject("okButton.Font")));
			this.okButton.Image = ((System.Drawing.Image)(resources.GetObject("okButton.Image")));
			this.okButton.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("okButton.ImageAlign")));
			this.okButton.ImageIndex = ((int)(resources.GetObject("okButton.ImageIndex")));
			this.okButton.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("okButton.ImeMode")));
			this.okButton.Location = ((System.Drawing.Point)(resources.GetObject("okButton.Location")));
			this.okButton.Name = "okButton";
			this.okButton.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("okButton.RightToLeft")));
			this.okButton.Size = ((System.Drawing.Size)(resources.GetObject("okButton.Size")));
			this.okButton.TabIndex = ((int)(resources.GetObject("okButton.TabIndex")));
			this.okButton.Text = resources.GetString("okButton.Text");
			this.okButton.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("okButton.TextAlign")));
			this.okButton.Visible = ((bool)(resources.GetObject("okButton.Visible")));
			this.okButton.Click += new System.EventHandler(this.OkButtonClick);
			// 
			// preferredCaptionLabel
			// 
			this.preferredCaptionLabel.AccessibleDescription = resources.GetString("preferredCaptionLabel.AccessibleDescription");
			this.preferredCaptionLabel.AccessibleName = resources.GetString("preferredCaptionLabel.AccessibleName");
			this.preferredCaptionLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("preferredCaptionLabel.Anchor")));
			this.preferredCaptionLabel.AutoSize = ((bool)(resources.GetObject("preferredCaptionLabel.AutoSize")));
			this.preferredCaptionLabel.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("preferredCaptionLabel.Dock")));
			this.preferredCaptionLabel.Enabled = ((bool)(resources.GetObject("preferredCaptionLabel.Enabled")));
			this.preferredCaptionLabel.Font = ((System.Drawing.Font)(resources.GetObject("preferredCaptionLabel.Font")));
			this.preferredCaptionLabel.Image = ((System.Drawing.Image)(resources.GetObject("preferredCaptionLabel.Image")));
			this.preferredCaptionLabel.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("preferredCaptionLabel.ImageAlign")));
			this.preferredCaptionLabel.ImageIndex = ((int)(resources.GetObject("preferredCaptionLabel.ImageIndex")));
			this.preferredCaptionLabel.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("preferredCaptionLabel.ImeMode")));
			this.preferredCaptionLabel.Location = ((System.Drawing.Point)(resources.GetObject("preferredCaptionLabel.Location")));
			this.preferredCaptionLabel.Name = "preferredCaptionLabel";
			this.preferredCaptionLabel.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("preferredCaptionLabel.RightToLeft")));
			this.preferredCaptionLabel.Size = ((System.Drawing.Size)(resources.GetObject("preferredCaptionLabel.Size")));
			this.preferredCaptionLabel.TabIndex = ((int)(resources.GetObject("preferredCaptionLabel.TabIndex")));
			this.preferredCaptionLabel.Text = resources.GetString("preferredCaptionLabel.Text");
			this.preferredCaptionLabel.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("preferredCaptionLabel.TextAlign")));
			this.preferredCaptionLabel.Visible = ((bool)(resources.GetObject("preferredCaptionLabel.Visible")));
			// 
			// preferedCaptionTextBox
			// 
			this.preferedCaptionTextBox.AccessibleDescription = resources.GetString("preferedCaptionTextBox.AccessibleDescription");
			this.preferedCaptionTextBox.AccessibleName = resources.GetString("preferedCaptionTextBox.AccessibleName");
			this.preferedCaptionTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("preferedCaptionTextBox.Anchor")));
			this.preferedCaptionTextBox.AutoSize = ((bool)(resources.GetObject("preferedCaptionTextBox.AutoSize")));
			this.preferedCaptionTextBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("preferedCaptionTextBox.BackgroundImage")));
			this.preferedCaptionTextBox.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("preferedCaptionTextBox.Dock")));
			this.preferedCaptionTextBox.Enabled = ((bool)(resources.GetObject("preferedCaptionTextBox.Enabled")));
			this.preferedCaptionTextBox.Font = ((System.Drawing.Font)(resources.GetObject("preferedCaptionTextBox.Font")));
			this.preferedCaptionTextBox.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("preferedCaptionTextBox.ImeMode")));
			this.preferedCaptionTextBox.Location = ((System.Drawing.Point)(resources.GetObject("preferedCaptionTextBox.Location")));
			this.preferedCaptionTextBox.MaxLength = ((int)(resources.GetObject("preferedCaptionTextBox.MaxLength")));
			this.preferedCaptionTextBox.Multiline = ((bool)(resources.GetObject("preferedCaptionTextBox.Multiline")));
			this.preferedCaptionTextBox.Name = "preferedCaptionTextBox";
			this.preferedCaptionTextBox.PasswordChar = ((char)(resources.GetObject("preferedCaptionTextBox.PasswordChar")));
			this.preferedCaptionTextBox.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("preferedCaptionTextBox.RightToLeft")));
			this.preferedCaptionTextBox.ScrollBars = ((System.Windows.Forms.ScrollBars)(resources.GetObject("preferedCaptionTextBox.ScrollBars")));
			this.preferedCaptionTextBox.Size = ((System.Drawing.Size)(resources.GetObject("preferedCaptionTextBox.Size")));
			this.preferedCaptionTextBox.TabIndex = ((int)(resources.GetObject("preferedCaptionTextBox.TabIndex")));
			this.preferedCaptionTextBox.Text = resources.GetString("preferedCaptionTextBox.Text");
			this.preferedCaptionTextBox.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("preferedCaptionTextBox.TextAlign")));
			this.preferedCaptionTextBox.Visible = ((bool)(resources.GetObject("preferedCaptionTextBox.Visible")));
			this.preferedCaptionTextBox.WordWrap = ((bool)(resources.GetObject("preferedCaptionTextBox.WordWrap")));
			this.preferedCaptionTextBox.TextChanged += new System.EventHandler(this.OnCaptionChanged);
			// 
			// ColumnSettingsUI
			// 
			this.AcceptButton = this.okButton;
			this.AccessibleDescription = resources.GetString("$this.AccessibleDescription");
			this.AccessibleName = resources.GetString("$this.AccessibleName");
			this.AutoScaleBaseSize = ((System.Drawing.Size)(resources.GetObject("$this.AutoScaleBaseSize")));
			this.AutoScroll = ((bool)(resources.GetObject("$this.AutoScroll")));
			this.AutoScrollMargin = ((System.Drawing.Size)(resources.GetObject("$this.AutoScrollMargin")));
			this.AutoScrollMinSize = ((System.Drawing.Size)(resources.GetObject("$this.AutoScrollMinSize")));
			this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
			this.CancelButton = this.cancelButton;
			this.ClientSize = ((System.Drawing.Size)(resources.GetObject("$this.ClientSize")));
			this.Controls.Add(this.preferedCaptionTextBox);
			this.Controls.Add(this.columnWidthTextBox);
			this.Controls.Add(this.preferredCaptionLabel);
			this.Controls.Add(this.okButton);
			this.Controls.Add(this.cancelButton);
			this.Controls.Add(this.widthLabel);
			this.Controls.Add(this.separatorGroupBox);
			this.Controls.Add(this.hideButton);
			this.Controls.Add(this.showButton);
			this.Controls.Add(this.moveDownButton);
			this.Controls.Add(this.moveUpButton);
			this.Controls.Add(this.columnsLabel);
			this.Controls.Add(this.columnsListView);
			this.Controls.Add(this.infoLabel);
			this.Enabled = ((bool)(resources.GetObject("$this.Enabled")));
			this.Font = ((System.Drawing.Font)(resources.GetObject("$this.Font")));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("$this.ImeMode")));
			this.Location = ((System.Drawing.Point)(resources.GetObject("$this.Location")));
			this.MaximizeBox = false;
			this.MaximumSize = ((System.Drawing.Size)(resources.GetObject("$this.MaximumSize")));
			this.MinimizeBox = false;
			this.MinimumSize = ((System.Drawing.Size)(resources.GetObject("$this.MinimumSize")));
			this.Name = "ColumnSettingsUI";
			this.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("$this.RightToLeft")));
			this.StartPosition = ((System.Windows.Forms.FormStartPosition)(resources.GetObject("$this.StartPosition")));
			this.Text = resources.GetString("$this.Text");
			this.ResumeLayout(false);

		}
		#endregion


		/// <summary>
		/// Swaps the two adjacent elements.
		/// </summary>
		/// <param name="index">The index of the item to swap.</param>
		private void SwapElements(int index)
		{
			if ( index > 0 && index < columnsListView.Items.Count)
			{
				columnsListView.BeginUpdate();
				ListViewItem item1 = columnsListView.Items[index];
				ListViewItem item2 = columnsListView.Items[index -1];
				columnsListView.Items.RemoveAt(index);
				columnsListView.Items.RemoveAt(index - 1);
				columnsListView.Items.Insert(index-1,item1);
				columnsListView.Items.Insert(index,item2);

				RefreshDetails();
				columnsListView.Focus();
				columnsListView.EndUpdate();
			}
		}

		/// <summary>
		/// Called when the move down button is pressed.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void OnMoveDown(object sender, System.EventArgs e)
		{
			if ( columnsListView.SelectedIndices.Count > 0 )
			{
				// Moving down an element is the same as
				// moving up the next element
				SwapElements(columnsListView.SelectedIndices[0] + 1);
			}
		}

		/// <summary>
		/// Called when the move up button is pressed.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void OnMoveUp(object sender, System.EventArgs e)
		{
			if ( columnsListView.SelectedIndices.Count > 0 )
				SwapElements(columnsListView.SelectedIndices[0]);
		}



		


		private void OnSelectedIndexChanged(object sender, System.EventArgs e)
		{
			RefreshDetails();
		}

		private void RefreshDetails()
		{
			if ( columnsListView.SelectedIndices.Count > 0 )
			{
				suppressUpdates = true;
				int idx = (int)columnsListView.SelectedItems[0].Tag;
			
				ColumnDetails details = columnSettings.ColumnDetails[idx];
				columnWidthTextBox.Text = details.Width.ToString("d", CultureInfo.CurrentUICulture);
				preferedCaptionTextBox.Text = details.PreferredCaption;
				if ( details.Required == true )
				{
					EnableButtons(ButtonEnabledState.None);
				}
				else
				{
					if ( columnsListView.SelectedItems[0].Checked )
						EnableButtons(ButtonEnabledState.Hide);
					else
						EnableButtons(ButtonEnabledState.Show);
				}
				suppressUpdates = false;

			}
		}

		private void OnWidthChanged(object sender, System.EventArgs e)
		{
			if (!suppressUpdates && columnsListView.SelectedIndices.Count > 0 && columnWidthTextBox.Text.Length > 0)
			{
				int idx = (int)columnsListView.SelectedItems[0].Tag;
				columnSettings.ColumnDetails[idx].Width = Convert.ToInt32(columnWidthTextBox.Text, CultureInfo.CurrentUICulture);
			}
		}

		private void OnCaptionChanged(object sender, System.EventArgs e)
		{
			if (!suppressUpdates && columnsListView.SelectedIndices.Count > 0 && preferedCaptionTextBox.Text.Length > 0)
			{
				int idx = (int)columnsListView.SelectedItems[0].Tag;
				columnSettings.ColumnDetails[idx].PreferredCaption = preferedCaptionTextBox.Text;
				columnsListView.Items[idx].Text = preferedCaptionTextBox.Text;
			}
		
		}

		private void ShowButtonClick(object sender, System.EventArgs e)
		{
			if ( columnsListView.SelectedIndices.Count > 0 )
			{
				columnsListView.SelectedItems[0].Checked = true;
				int idx = (int)columnsListView.SelectedItems[0].Tag;

				columnSettings.ColumnDetails[idx].Show = true;
				EnableButtons(ButtonEnabledState.Hide);
				columnsListView.Focus();
			}
		}

		private void HideButtonClick(object sender, System.EventArgs e)
		{
			if ( columnsListView.SelectedIndices.Count > 0 )
			{
				columnsListView.SelectedItems[0].Checked = false;
				int idx = (int)columnsListView.SelectedItems[0].Tag;

				columnSettings.ColumnDetails[idx].Show = false;
				EnableButtons(ButtonEnabledState.Show);
				columnsListView.Focus();
			}
		}

		private void EnableButtons(ButtonEnabledState e)
		{
			switch(e)
			{
				case ButtonEnabledState.Show:
					showButton.Enabled = true;
					hideButton.Enabled = false;
					break;
				case ButtonEnabledState.Hide:
					showButton.Enabled = false;
					hideButton.Enabled = true;
					break;
				default:
					showButton.Enabled = false;
					hideButton.Enabled = false;
					break;
			}

		}

		private void OnItemChecked(object sender, System.Windows.Forms.ItemCheckEventArgs e)
		{
			int idx = e.Index;
			if ( columnSettings.ColumnDetails[idx].Required == true)
			{
				// Fields that are required should remain checked
				e.NewValue = CheckState.Checked;
			}
		}

		private void OkButtonClick(object sender, System.EventArgs e)
		{
			columnSettings.ReIndexPositions();
		}

		
	}
}

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
n.runs
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions