views:

361

answers:

4

hello, i have a windowsfroms 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 winforms to remember the screenlocation and windowsize (and if possible the windowstate) or does everything have to be done by hand?

thanks!

+1  A: 

You'll have to manually save the information somewhere. I'd suggest doing so as application settings, storing them in user specific isolated storage.

Once you load up, read the settings then resize/move your form.

Ian
+3  A: 

You'll need to save the window location and size in your application settings. Here's a good C# article to show you how.

EDIT

You can save pretty much anything you want in the application settings. In the Type column of the settings grid you can browse to any .NET type. WindowState is in System.Windows.Forms and is listed as FormWindowState. There's also a property for FormStartPosition.

Walter
+1 nice example.
Ian
thanks, that is a good article. only thing left is to remember the windowstate (maximized, minimized, ...) how can i put that one to the settings?
clamp
Set it up using an enum?
Ian
yes thanks. i didnt realize that you can browse to all types of .NET in these settings at first.
clamp
thanks! one more thing: if the window is maximized, how can i remember which monitor it is maximized on in a multi-monitor setup?
clamp
Add the monitor id to the application settings as well.
Walter
thanks, but i couldnt find a way to get the monitor id. i only found the Screen object, but it doesnt seem to have something unique associated with it.
clamp
+2  A: 

Matt - to save the WindowState as a user setting, in the Settings Dialog, in the "Type" dropdown, scroll to the bottom and select "Browse".

In the "Select a Type" dialog, expand System.Windows.Forms and you can choose "FormWindowState" as the type.

(sorry, I don't see a button that allows me to comment on the comment...)

JohnForDummies
Again, I'm sorry, I can't add a comment to the other thread... But Matt, by setting the location of the form, it will move it to the correct screen, and then you can set the WindowState to Maximized, etc, and it will be on the correct screen.
JohnForDummies
+1  A: 

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;
        }
        else
        {
            Properties.Settings.Default.Location = Location;
            Properties.Settings.Default.Size = Size;
            Properties.Settings.Default.Maximised = false;
        }
        Properties.Settings.Default.Save();

It will save the current state.

Then add this code to your constructor:

        if (Properties.Settings.Default.Maximised)
        {
            WindowState = FormWindowState.Maximized;
            Location = Properties.Settings.Default.Location;
            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.

ChrisF