Restore Form Size and Position in C#
The below is the code used for Save/Restore the Form Size and Position(ie. last viewed position and size of the Form).
In Settings.settings file, we should add the following NAME and TYPE. WindowState - string, WindowWidth - int, WindowHeight - int, WindowLocationX - int, WindowLocationY - int.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Xml;
using ShowWindow.Properties;
using System.Globalization;
namespace ShowWindow
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
RestoreWindow();
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
this.LocationChanged += new EventHandler(Form1_LocationChanged);
}
private void RestoreWindow()
{
if (Properties.Settings.Default.WindowHeight == 0
|| Properties.Settings.Default.WindowWidth == 0)
{
return;
}
this.Height = Properties.Settings.Default.WindowHeight;
this.Width = Properties.Settings.Default.WindowWidth;
Point windowPoint = new Point(Properties.Settings.Default.WindowLocationX,
Properties.Settings.Default.WindowLocationY);
this.Location = windowPoint;
this.StartPosition = FormStartPosition.Manual;
if (Properties.Settings.Default.WindowState == "Normal")
{
this.WindowState = FormWindowState.Normal;
}
else if (Properties.Settings.Default.WindowState == "Maximized")
{
this.WindowState = FormWindowState.Maximized;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.WindowWidth = this.Width;
Properties.Settings.Default.WindowHeight = this.Height;
Properties.Settings.Default.WindowLocationX = this.Location.X;
Properties.Settings.Default.WindowLocationY = this.Location.Y;
Properties.Settings.Default.WindowState = Convert.ToString(this.WindowState, CultureInfo.CurrentCulture);
Properties.Settings.Default.Save();
}
}
}
using ShowWindow.Properties;
how can i get this namespace????