Resources » Code Snippets » Application windows, menus & toolbars

Restore Form Size and Position in C#


Last Updated:   Category: Application windows, menus & toolbars    
Author: Member Level: Gold    Points: 8



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();
}
}
}


Did you like this resource? Share it with your friends and show your love!

Responses to "Restore Form Size and Position in C#"
Guest Author: len     06 Mar 2013
using ShowWindow.Properties;
how can i get this namespace????



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India