Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET / Windows Forms: remember windows size and location

I have a Windows Forms application with a normal window. Now when I close the application and restart it, I want that the main window appears at the same location on my screen with the same size of the moment when it was closed.

Is there an easy way in Windows Forms to remember the screen location and window size (and if possible the window state) or does everything have to be done by hand?

like image 544
clamp Avatar asked Dec 09 '09 12:12

clamp


People also ask

How do I make my windows form resizable?

To make a control resize with the form, set the Anchor to all four sides, or set Dock to Fill . It's fairly intuitive once you start using it. For example, if you have OK/Cancel buttons in the lower right of your dialog, set the Anchor property to Bottom and Right to have them drag properly on the form.

How can you save the desired properties of Windows Forms application?

A simple way is to use a configuration data object, save it as an XML file with the name of the application in the local Folder and on startup read it back.

How do I fit windows form to any resolution?

simply set Autoscroll = true for ur windows form..


1 Answers

If you add this code to your FormClosing event handler:

if (WindowState == FormWindowState.Maximized) {     Properties.Settings.Default.Location = RestoreBounds.Location;     Properties.Settings.Default.Size = RestoreBounds.Size;     Properties.Settings.Default.Maximised = true;     Properties.Settings.Default.Minimised = false; } else if (WindowState == FormWindowState.Normal) {     Properties.Settings.Default.Location = Location;     Properties.Settings.Default.Size = Size;     Properties.Settings.Default.Maximised = false;     Properties.Settings.Default.Minimised = false; } else {     Properties.Settings.Default.Location = RestoreBounds.Location;     Properties.Settings.Default.Size = RestoreBounds.Size;     Properties.Settings.Default.Maximised = false;     Properties.Settings.Default.Minimised = true; } Properties.Settings.Default.Save(); 

It will save the current state.

Then add this code to your form's OnLoad handler:

if (Properties.Settings.Default.Maximised) {     Location = Properties.Settings.Default.Location;     WindowState = FormWindowState.Maximized;     Size = Properties.Settings.Default.Size; } else if (Properties.Settings.Default.Minimised) {     Location = Properties.Settings.Default.Location;     WindowState = FormWindowState.Minimized;     Size = Properties.Settings.Default.Size; } else {     Location = Properties.Settings.Default.Location;     Size = Properties.Settings.Default.Size; } 

It will restore the last state.

It even remembers which monitor in a multi monitor set up the application was maximised to.

like image 175
ChrisF Avatar answered Sep 18 '22 14:09

ChrisF