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?
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.
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.
simply set Autoscroll = true for ur windows form..
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With