C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Application windows, menus & toolbars »

Restore Form Size and Position in C#


Posted Date: 31 Oct 2008    Resource Type: Code Snippets    Category: Application windows, menus & toolbars
Author: Saravanan KanagarasuMember Level: Gold    
Rating: 1 out of 5Points: 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();
}
}
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Save/Restore Form Size in C#  .  Restore Form/Window Size and Position in C#  .  Position in C#  .  Last Visited Form Size  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: List out Directory and Files in the Drive with size in Tree formate
Previous Resource: Device Application - Single instance of the EXE
Return to Discussion Resource Index
Post New Resource
Category: Application windows, menus & toolbars


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use