Click here to Skip to main content
15,867,835 members
Articles / Programming Languages / C#

Saving and Restoring Application Settings

Rate me:
Please Sign up or sign in to vote.
4.78/5 (38 votes)
15 Mar 20034 min read 128K   3.4K   85  
The Savior class makes it simple to save and restore application settings using the registry or a binary file.
// =====================================================================
//
// Savior - Simplify Saving and Restoring Application Settings
//
// by Jim Hollenhorst, jim@ultrapico.com
// Copyright Ultrapico, March 2003
// http://www.ultrapico.com
//
// =====================================================================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
using System.Globalization;
using System.Text;
using System.IO;
using SaveListView;
using SaviorClass;

namespace SaveAndRestore
{
	/// This is a test application to show the use of the Savior class to save and restore
	/// application settings. All the methods of Savior are static. They work in conjunction
	/// with a user-defined class (here called Settings), which is used to store all
	/// application settings. The Savior class is then used to read or save the settings from
	/// a file or from the registry.
	
	/// <summary>
	/// This is an example of an enum. Savior makes it easy to save and restore enums.
	/// </summary>
	public enum Thing {Dog,Cat,Mouse,Bat,Frog,Fly};
	
	
	/// <summary>
	/// SaviorTest is the test application to show the use of Savior and Settings.
	/// </summary>
	public class SaviorTest : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.CheckBox checkBox1;
		private System.Windows.Forms.NumericUpDown numericUpDown1;
		private System.Windows.Forms.Button SaveBtn;
		private System.Windows.Forms.Button ReadBtn;
		private System.Windows.Forms.Button ShowBtn;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ColumnHeader columnHeader4;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ListBox EnumBox;
		private System.Windows.Forms.Button ColorBtn;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.ColorDialog ColorDialog;
		private System.Windows.Forms.Label ColorLabel;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.ListBox StringArrayBox;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.TextBox ByteArrayBox;
		private System.Windows.Forms.Button SaveFileBtn;
		private System.Windows.Forms.Button ReadFileBtn;
		private System.Windows.Forms.SaveFileDialog SaveDialog;
		private System.Windows.Forms.OpenFileDialog OpenDialog;
		private System.Windows.Forms.Button ExitBtn;
		private Settings settings;

		public SaviorTest()
		{
			InitializeComponent();

			// Create an instance of the Settings class, which will hold all application settings
			settings = new Settings();

			// Set up the initial directories and file names for the Open and Save file dialogs
			FileInfo fi = new FileInfo(Application.ExecutablePath);
			OpenDialog.InitialDirectory=fi.DirectoryName;
			OpenDialog.FileName="settings.bin";
			SaveDialog.InitialDirectory=fi.DirectoryName;
			SaveDialog.FileName="settings.bin";

			// Add the "Thing" enum value names to the Enum list box
			foreach(string name in Enum.GetNames(typeof(Thing)))
			{
				EnumBox.Items.Add(name);
			}
			
			// Read the settings on startup
			this.Read();
		}

		/// <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.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new System.Windows.Forms.ListViewItem.ListViewSubItem[] {
																																																																												new System.Windows.Forms.ListViewItem.ListViewSubItem(null, "Dog", System.Drawing.SystemColors.WindowText, System.Drawing.SystemColors.Window, new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)))),
																																																																												new System.Windows.Forms.ListViewItem.ListViewSubItem(null, "Rover"),
																																																																												new System.Windows.Forms.ListViewItem.ListViewSubItem(null, "12"),
																																																																												new System.Windows.Forms.ListViewItem.ListViewSubItem(null, "Luke")}, -1);
			System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem(new System.Windows.Forms.ListViewItem.ListViewSubItem[] {
																																																																												new System.Windows.Forms.ListViewItem.ListViewSubItem(null, "Cat", System.Drawing.SystemColors.WindowText, System.Drawing.SystemColors.Window, new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)))),
																																																																												new System.Windows.Forms.ListViewItem.ListViewSubItem(null, "Fluffy"),
																																																																												new System.Windows.Forms.ListViewItem.ListViewSubItem(null, "3"),
																																																																												new System.Windows.Forms.ListViewItem.ListViewSubItem(null, "Maynard")}, -1);
			this.label1 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.checkBox1 = new System.Windows.Forms.CheckBox();
			this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
			this.SaveBtn = new System.Windows.Forms.Button();
			this.ReadBtn = new System.Windows.Forms.Button();
			this.ShowBtn = new System.Windows.Forms.Button();
			this.ColorDialog = new System.Windows.Forms.ColorDialog();
			this.ColorBtn = new System.Windows.Forms.Button();
			this.panel1 = new System.Windows.Forms.Panel();
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.EnumBox = new System.Windows.Forms.ListBox();
			this.label5 = new System.Windows.Forms.Label();
			this.ColorLabel = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.StringArrayBox = new System.Windows.Forms.ListBox();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.ByteArrayBox = new System.Windows.Forms.TextBox();
			this.SaveFileBtn = new System.Windows.Forms.Button();
			this.ReadFileBtn = new System.Windows.Forms.Button();
			this.SaveDialog = new System.Windows.Forms.SaveFileDialog();
			this.OpenDialog = new System.Windows.Forms.OpenFileDialog();
			this.ExitBtn = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
			this.panel1.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(12, 20);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(489, 34);
			this.label1.TabIndex = 0;
			this.label1.Text = "Make changes to the items below, then save or read the settings. Also try changin" +
				"g the Location and Size of the form.";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(10, 62);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(375, 20);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "A Text Box";
			// 
			// checkBox1
			// 
			this.checkBox1.Location = new System.Drawing.Point(11, 99);
			this.checkBox1.Name = "checkBox1";
			this.checkBox1.TabIndex = 2;
			this.checkBox1.Text = "A Check Box";
			// 
			// numericUpDown1
			// 
			this.numericUpDown1.Location = new System.Drawing.Point(11, 129);
			this.numericUpDown1.Name = "numericUpDown1";
			this.numericUpDown1.Size = new System.Drawing.Size(78, 20);
			this.numericUpDown1.TabIndex = 4;
			// 
			// SaveBtn
			// 
			this.SaveBtn.Location = new System.Drawing.Point(398, 74);
			this.SaveBtn.Name = "SaveBtn";
			this.SaveBtn.Size = new System.Drawing.Size(118, 23);
			this.SaveBtn.TabIndex = 5;
			this.SaveBtn.Text = "&Save to Registry";
			this.SaveBtn.Click += new System.EventHandler(this.SaveBtn_Click);
			// 
			// ReadBtn
			// 
			this.ReadBtn.Location = new System.Drawing.Point(398, 109);
			this.ReadBtn.Name = "ReadBtn";
			this.ReadBtn.Size = new System.Drawing.Size(118, 23);
			this.ReadBtn.TabIndex = 6;
			this.ReadBtn.Text = "&Read From Registry";
			this.ReadBtn.Click += new System.EventHandler(this.ReadBtn_Click);
			// 
			// ShowBtn
			// 
			this.ShowBtn.Location = new System.Drawing.Point(398, 214);
			this.ShowBtn.Name = "ShowBtn";
			this.ShowBtn.Size = new System.Drawing.Size(118, 23);
			this.ShowBtn.TabIndex = 7;
			this.ShowBtn.Text = "Sho&w Settings";
			this.ShowBtn.Click += new System.EventHandler(this.ShowBtn_Click);
			// 
			// ColorBtn
			// 
			this.ColorBtn.BackColor = System.Drawing.SystemColors.Control;
			this.ColorBtn.Location = new System.Drawing.Point(62, 9);
			this.ColorBtn.Name = "ColorBtn";
			this.ColorBtn.Size = new System.Drawing.Size(90, 23);
			this.ColorBtn.TabIndex = 9;
			this.ColorBtn.Text = "&Change Color";
			this.ColorBtn.Click += new System.EventHandler(this.ColorBtn_Click);
			// 
			// panel1
			// 
			this.panel1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
			this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
																																				 this.ColorBtn});
			this.panel1.Location = new System.Drawing.Point(166, 110);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(221, 48);
			this.panel1.TabIndex = 10;
			// 
			// listView1
			// 
			this.listView1.AllowColumnReorder = true;
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																																								this.columnHeader1,
																																								this.columnHeader2,
																																								this.columnHeader3,
																																								this.columnHeader4});
			this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
																																							listViewItem1,
																																							listViewItem2});
			this.listView1.Location = new System.Drawing.Point(6, 186);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(383, 59);
			this.listView1.TabIndex = 11;
			this.listView1.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Type";
			this.columnHeader1.Width = 81;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "Name";
			this.columnHeader2.Width = 85;
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "Age";
			this.columnHeader3.Width = 84;
			// 
			// columnHeader4
			// 
			this.columnHeader4.Text = "Master";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(5, 165);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(367, 17);
			this.label2.TabIndex = 12;
			this.label2.Text = "Try changing column widths and rearranging column order";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(96, 129);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 23);
			this.label3.TabIndex = 13;
			this.label3.Text = "A Decimal";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// EnumBox
			// 
			this.EnumBox.Location = new System.Drawing.Point(10, 43);
			this.EnumBox.Name = "EnumBox";
			this.EnumBox.Size = new System.Drawing.Size(80, 95);
			this.EnumBox.TabIndex = 14;
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(10, 23);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(86, 16);
			this.label5.TabIndex = 16;
			this.label5.Text = "Select a \"Thing\"";
			// 
			// ColorLabel
			// 
			this.ColorLabel.Location = new System.Drawing.Point(166, 89);
			this.ColorLabel.Name = "ColorLabel";
			this.ColorLabel.Size = new System.Drawing.Size(214, 15);
			this.ColorLabel.TabIndex = 10;
			this.ColorLabel.Text = "Color";
			this.ColorLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																																						this.EnumBox,
																																						this.label5});
			this.groupBox1.Location = new System.Drawing.Point(8, 254);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(103, 149);
			this.groupBox1.TabIndex = 17;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Enum Example";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																																						this.StringArrayBox});
			this.groupBox2.Location = new System.Drawing.Point(252, 254);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(139, 149);
			this.groupBox2.TabIndex = 18;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Strings Example";
			// 
			// StringArrayBox
			// 
			this.StringArrayBox.Items.AddRange(new object[] {
																												"You can\'t change",
																												"these but you can",
																												"check to see if they",
																												"are being saved and",
																												"restored properly!"});
			this.StringArrayBox.Location = new System.Drawing.Point(11, 26);
			this.StringArrayBox.Name = "StringArrayBox";
			this.StringArrayBox.Size = new System.Drawing.Size(117, 108);
			this.StringArrayBox.TabIndex = 0;
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																																						this.ByteArrayBox});
			this.groupBox3.Location = new System.Drawing.Point(119, 254);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(125, 149);
			this.groupBox3.TabIndex = 19;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Byte Array Example";
			// 
			// ByteArrayBox
			// 
			this.ByteArrayBox.Location = new System.Drawing.Point(13, 26);
			this.ByteArrayBox.Multiline = true;
			this.ByteArrayBox.Name = "ByteArrayBox";
			this.ByteArrayBox.Size = new System.Drawing.Size(98, 108);
			this.ByteArrayBox.TabIndex = 0;
			this.ByteArrayBox.Text = "textBox2";
			// 
			// SaveFileBtn
			// 
			this.SaveFileBtn.Location = new System.Drawing.Point(398, 144);
			this.SaveFileBtn.Name = "SaveFileBtn";
			this.SaveFileBtn.Size = new System.Drawing.Size(118, 23);
			this.SaveFileBtn.TabIndex = 20;
			this.SaveFileBtn.Text = "S&ave to File";
			this.SaveFileBtn.Click += new System.EventHandler(this.SaveFileBtn_Click);
			// 
			// ReadFileBtn
			// 
			this.ReadFileBtn.Location = new System.Drawing.Point(398, 179);
			this.ReadFileBtn.Name = "ReadFileBtn";
			this.ReadFileBtn.Size = new System.Drawing.Size(118, 23);
			this.ReadFileBtn.TabIndex = 21;
			this.ReadFileBtn.Text = "R&ead From File";
			this.ReadFileBtn.Click += new System.EventHandler(this.ReadFileBtn_Click);
			// 
			// SaveDialog
			// 
			this.SaveDialog.DefaultExt = "bin";
			this.SaveDialog.FileName = "doc1";
			this.SaveDialog.Filter = "Binary File (*.bin)|*.bin|All Files (*.*)|*.*";
			this.SaveDialog.Title = "Save Application Settings to a File";
			// 
			// OpenDialog
			// 
			this.OpenDialog.DefaultExt = "bin";
			this.OpenDialog.Filter = "Binary File (*.bin)|*.bin|All Files (*.*)|*.*";
			// 
			// ExitBtn
			// 
			this.ExitBtn.Location = new System.Drawing.Point(398, 249);
			this.ExitBtn.Name = "ExitBtn";
			this.ExitBtn.Size = new System.Drawing.Size(118, 23);
			this.ExitBtn.TabIndex = 22;
			this.ExitBtn.Text = "&Exit";
			this.ExitBtn.Click += new System.EventHandler(this.ExitBtn_Click);
			// 
			// SaviorTest
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.ExitBtn;
			this.ClientSize = new System.Drawing.Size(522, 408);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																																	this.ExitBtn,
																																	this.ReadFileBtn,
																																	this.SaveFileBtn,
																																	this.groupBox3,
																																	this.groupBox2,
																																	this.groupBox1,
																																	this.label3,
																																	this.label2,
																																	this.listView1,
																																	this.panel1,
																																	this.ShowBtn,
																																	this.ReadBtn,
																																	this.SaveBtn,
																																	this.numericUpDown1,
																																	this.checkBox1,
																																	this.textBox1,
																																	this.label1,
																																	this.ColorLabel});
			this.Name = "SaviorTest";
			this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
			this.Text = "Testing the \"Savior\" class";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.SaviorTest_Closing);
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
			this.panel1.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new SaviorTest());
		}

		/// <summary>
		/// Show the "ToString()" representation of the settings
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ShowBtn_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show("Value of application settings at the last Read or Save:\n\n"+
				Savior.ToString(settings));
		}

		/// <summary>
		/// Save all the applications settings to the "Settings" class, then save to the registry.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SaveBtn_Click(object sender, System.EventArgs e)
		{
			this.Save();
		}

		/// <summary>
		/// Save the application settings to the registry
		/// </summary>
		private void Save()
		{
			this.SetSettings();
			RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Ultrapico\\Savior");
			Savior.Save(settings,key);
		}

		/// <summary>
		/// Put all the application information into the Settings class
		/// </summary>
		private void SetSettings()
		{
			// All the values to be saved are stored in the class "Settings" that has been customized
			// for this application

			// Save a string, a boolean, and a decimal
			settings.Text=textBox1.Text;
			settings.Check=checkBox1.Checked;
			settings.TheNumber=numericUpDown1.Value;

			// Save the location and size of the application window
			settings.Location=this.Location;
			settings.TheSize=this.Size;

			// Save a color and an enum
			settings.BackColor=panel1.BackColor;
			settings.thing=(Thing)Enum.Parse(typeof(Thing),(string)EnumBox.SelectedItem);
			
			// Put the array of strings from the list box into the settings.SomeStrings array
			settings.SomeStrings= new string[StringArrayBox.Items.Count];
			StringArrayBox.Items.CopyTo(settings.SomeStrings,0);


			// Put the array of bytes from the list box into the settings.SomeBytes array
			settings.SomeBytes = GetBytesFromHex(ByteArrayBox.Text);

			// Save all the list view settings
			int[] ColOrders = ListViewSettings.GetColumnOrders(listView1);
			int NCols = ColOrders.Length;
			settings.Orders=new int[NCols];
			settings.Widths=new int[NCols];
			settings.Headers=new string[NCols];
			for(int i=0;i<NCols;i++)
			{
				settings.Orders[i]=ColOrders[i];
				settings.Widths[i]=listView1.Columns[i].Width;
				settings.Headers[i]=listView1.Columns[i].Text;
			}

			// Save an array of booleans
			settings.Truth= new Boolean[]{true,false,false,true};
		}

		/// <summary>
		/// Read all the application settings from the registry and then change the appropriate
		/// elements to display the new settings.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ReadBtn_Click(object sender, System.EventArgs e)
		{
			this.Read();
		}
		
		/// <summary>
		/// Read all the application settings from the registry and then change the appropriate
		/// elements to display the new settings.
		/// </summary>
		private void Read()
		{
			// Open or create the registry key in which to save application settings 
			RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Ultrapico\\Savior");

			// Read all of the settings from the registry
			Savior.Read(settings,key);

			// Update all the application information with values in the Settings class
			this.GetSettings();
		}

		/// <summary>
		/// Update all the application information with values in the Settings class
		/// </summary>
		private void GetSettings()
		{
			// Here all the application settings are restored by getting the updated values
			// from the "Settings" class

			// Set a string, a boolean, and a decimal
			textBox1.Text=settings.Text;
			checkBox1.Checked=settings.Check;
			numericUpDown1.Value=settings.TheNumber;

			// Set the location and size of the application window
			this.Location=settings.Location;
			this.Size=settings.TheSize;

			// Set a color and an enum
			panel1.BackColor=settings.BackColor;
			ColorLabel.Text="Color: "+settings.BackColor.Name;
			EnumBox.SelectedIndex=(int)settings.thing;


			// Put the string array into the StringArrayBox
			StringArrayBox.Items.Clear();
			StringArrayBox.Items.AddRange(settings.SomeStrings);

			// Put the byte array into the ByteArrayBox
			ByteArrayBox.Text=GetHexFromBytes(settings.SomeBytes);

			// Read all the settings associated with the multi-column ListView including
			// the proper order of the columns, the column widths, and the column headers
			int NCols=listView1.Columns.Count;
			int[] ColOrders = new int[NCols];
			for(int i=0;i<NCols;i++)
			{
				if(i>=settings.Orders.Length)break; // We must have added a column since the last save
				ColOrders[i]=settings.Orders[i];
				listView1.Columns[i].Width=settings.Widths[i];
				listView1.Columns[i].Text=settings.Headers[i];
			}
			ListViewSettings.SetColumnOrders(listView1,ColOrders);
		}

		/// <summary>
		/// When the color button is clicked, a color dialog is opened and the sample color is changed
		/// settings class.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ColorBtn_Click(object sender, System.EventArgs e)
		{
			if(ColorDialog.ShowDialog()==DialogResult.OK)
			{
				panel1.BackColor=ColorDialog.Color;
				ColorLabel.Text="Color: "+panel1.BackColor.Name;
			}
		}

		/// <summary>
		/// When the application closes, save the settings to the registry
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SaviorTest_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			this.Save();
		}

		/// <summary>
		/// Return an array of bytes from a hexadecimal string
		/// </summary>
		/// <param name="hex">A string of hexadecimal bytes separated by commas</param>
		/// <returns>An array of bytes</returns>
		private byte[] GetBytesFromHex(string hex)
		{
			hex=hex.Trim();  // Trim any leading or trailing blanks
			string[] HexBytes = hex.Split(" ".ToCharArray());
			int Number = HexBytes.Length;
			byte[] bytes = new byte[Number];

			for (int i=0; i<Number; i++)
				bytes[i] = Byte.Parse(HexBytes[i],NumberStyles.AllowHexSpecifier);
			return bytes;
		}

		/// <summary>
		/// Return a hexadecimal string representing an array of bytes
		/// </summary>
		/// <param name="bytes">An array of bytes</param>
		/// <returns>A comma separated hexadecimal string</returns>
		private string GetHexFromBytes(byte[] bytes)
		{
			StringBuilder Hex = new StringBuilder();
			foreach (byte b in bytes)
				Hex.Append(String.Format("{0:X2} ",b));
			return Hex.ToString();
		}

		/// <summary>
		/// Save the application settings to a file
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SaveFileBtn_Click(object sender, System.EventArgs e)
		{			
			if(SaveDialog.ShowDialog()==DialogResult.OK)
			{
				this.SetSettings();
				Savior.SaveToFile(settings,SaveDialog.FileName);
			}
		}

		/// <summary>
		/// Read the application settings from a file
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ReadFileBtn_Click(object sender, System.EventArgs e)
		{			
			if(OpenDialog.ShowDialog()==DialogResult.OK)
			{
				settings = (Settings)Savior.ReadFromFile(OpenDialog.FileName);
				this.GetSettings();
			}
		}

		private void ExitBtn_Click(object sender, System.EventArgs e)
		{
			this.Save();
			Application.Exit();
		}
	}
}

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
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.

Comments and Discussions