Click here to Skip to main content
15,894,410 members
Articles / Programming Languages / C#

Form Placement Component

Rate me:
Please Sign up or sign in to vote.
3.91/5 (11 votes)
11 Nov 20036 min read 64.3K   1.4K   33  
A component class that restores a form's placement (location, size and state) to what it was when it was last closed.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using System.Data;
using AMS.Profile;

namespace PlacementDemo
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private AMS.Form.Placement placement;
		private System.Windows.Forms.CheckBox chkRestoreLocation;
		private System.Windows.Forms.CheckBox chkRestoreSize;
		private System.Windows.Forms.CheckBox chkRestoreState;
		private System.Windows.Forms.Button btnRestart;
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.StatusBar statusBar;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			// Retrieve the settings from the profile (which we already have in the Placement object)
			// Note: This code is here only for the purposes of this demo
			IProfile profile = this.placement.Profile;
			this.placement.RestoreLocation = this.chkRestoreLocation.Checked = profile.GetValue(Text, "Restore Location", true);
			this.placement.RestoreSize = this.chkRestoreSize.Checked = profile.GetValue(Text, "Restore Size", true);
			this.placement.RestoreState = this.chkRestoreState.Checked = profile.GetValue(Text, "Restore State", true);
		}

		private void btnRestart_Click(object sender, System.EventArgs e)
		{
			btnExit_Click(sender, e);
			Process.Start(GetType().Assembly.Location);
		}

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			// Save the form's settings to the profile for next time
			// Note: This code is here only for the purposes of this demo
			IProfile profile = this.placement.Profile;
			profile.SetValue(Text, "Restore Location", this.chkRestoreLocation.Checked);
			profile.SetValue(Text, "Restore Size", this.chkRestoreSize.Checked);
			profile.SetValue(Text, "Restore State", this.chkRestoreState.Checked);

			Close();
		}

		/// <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()
		{
			this.placement = new AMS.Form.Placement();
			this.chkRestoreLocation = new System.Windows.Forms.CheckBox();
			this.chkRestoreSize = new System.Windows.Forms.CheckBox();
			this.chkRestoreState = new System.Windows.Forms.CheckBox();
			this.btnRestart = new System.Windows.Forms.Button();
			this.btnExit = new System.Windows.Forms.Button();
			this.statusBar = new System.Windows.Forms.StatusBar();
			this.SuspendLayout();
			// 
			// placement
			// 
			this.placement.Form = this;
			this.placement.RestoreLocation = true;
			this.placement.RestoreSize = true;
			this.placement.RestoreState = true;
			this.placement.Section = "Placement Demo";
			// 
			// chkRestoreLocation
			// 
			this.chkRestoreLocation.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.chkRestoreLocation.Checked = true;
			this.chkRestoreLocation.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkRestoreLocation.Location = new System.Drawing.Point(6, 12);
			this.chkRestoreLocation.Name = "chkRestoreLocation";
			this.chkRestoreLocation.Size = new System.Drawing.Size(114, 18);
			this.chkRestoreLocation.TabIndex = 1;
			this.chkRestoreLocation.Text = "Restore Location:";
			// 
			// chkRestoreSize
			// 
			this.chkRestoreSize.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.chkRestoreSize.Checked = true;
			this.chkRestoreSize.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkRestoreSize.Location = new System.Drawing.Point(6, 39);
			this.chkRestoreSize.Name = "chkRestoreSize";
			this.chkRestoreSize.Size = new System.Drawing.Size(114, 18);
			this.chkRestoreSize.TabIndex = 2;
			this.chkRestoreSize.Text = "Restore Size:";
			// 
			// chkRestoreState
			// 
			this.chkRestoreState.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.chkRestoreState.Checked = true;
			this.chkRestoreState.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkRestoreState.Location = new System.Drawing.Point(6, 66);
			this.chkRestoreState.Name = "chkRestoreState";
			this.chkRestoreState.Size = new System.Drawing.Size(114, 18);
			this.chkRestoreState.TabIndex = 3;
			this.chkRestoreState.Text = "Restore State:";
			// 
			// btnRestart
			// 
			this.btnRestart.Location = new System.Drawing.Point(153, 12);
			this.btnRestart.Name = "btnRestart";
			this.btnRestart.Size = new System.Drawing.Size(105, 27);
			this.btnRestart.TabIndex = 4;
			this.btnRestart.Text = "Exit and Restart";
			this.btnRestart.Click += new System.EventHandler(this.btnRestart_Click);
			// 
			// btnExit
			// 
			this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnExit.Location = new System.Drawing.Point(153, 54);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(105, 27);
			this.btnExit.TabIndex = 5;
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// statusBar
			// 
			this.statusBar.Location = new System.Drawing.Point(0, 80);
			this.statusBar.Name = "statusBar";
			this.statusBar.Size = new System.Drawing.Size(269, 11);
			this.statusBar.TabIndex = 6;
			// 
			// MainForm
			// 
			this.AcceptButton = this.btnRestart;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnExit;
			this.ClientSize = new System.Drawing.Size(269, 91);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnExit,
																		  this.btnRestart,
																		  this.chkRestoreState,
																		  this.chkRestoreSize,
																		  this.chkRestoreLocation,
																		  this.statusBar});
			this.Name = "MainForm";
			this.Text = "Placement Demo";
			this.ResumeLayout(false);

		}
		#endregion

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

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've done extensive work with C++, MFC, COM, and ATL on the Windows side. On the Web side, I've worked with VB, ASP, JavaScript, and COM+. I've also been involved with server-side Java, which includes JSP, Servlets, and EJB, and more recently with ASP.NET/C#.

Comments and Discussions