• Save and restore your form size and location
  • OriginalGriff
  • The Nuggets translation Project

One of the things that really bothers me — and probably for you — is having five or so Windows in the right place on the desktop, and they’re cute. And then you open them tomorrow… One of them is always too big when opened and in the middle of the screen…

introduce

It’s really easy to save and restore the size and position of a form — it only takes a few lines of code — but it’s easy to forget that it does have an impact on the “professional” look of your application. It can be frustrating to try to organize tasks on your desktop, but it often gets scrambled because you have to move applications to where they belong……

So, if it’s easy, why don’t you implement it?

Use the code

There are only three things you need to do:

  1. Create a place to hold size and location information
  2. Restores information at startup
  3. Save information when closing

A place to store location and size information

The easiest place to store this information is in an application configuration file:

  • In Visual Studio, open the project branch in Solution Explorer.
  • Open the Properties subbranch and double-click the Settings.Settings node.
  • The project will be openedSettingsPage. Add a new entry: “InitialLocation” — keep the “string” type and the “User” range (you can also type “Value”, but you really don’t need to).

  • Save and close the page.

Restores position and size

This is also easy to do — all you need to do is set the Form’s properties, otherwise the display will look a bit messy when it opens. In Visual Studio Designer, set the StartPosition of the Form to Manual. If you forget this, Windows will locate the form where it sees fit and then move it to the location of your choice.

Note: The StartPosition property can also be set directly into the form’s constructor, as shown below.

public Form1()
{
    InitializeComponent();
    this.StartPosition = FormStartPosition.Manual;
    // this.Location = new Point(0, 0); // Location sets the Location, which is handled in the Load event below
}
Copy the code

Then all you need to do is restore the code. Handle in the form.load event:

private void frmMain_Load(object sender, EventArgs e)
{
    if ((ModifierKeys & Keys.Shift) == 0)
    {
        string initLocation = Properties.Settings.Default.InitialLocation;
        Point il = new Point(0.0);
        Size sz = Size;
        if (!string.IsNullOrWhiteSpace(initLocation))
        {
            string[] parts = initLocation.Split(', ');
            if (parts.Length >= 2)
            {
                il = new Point(int.Parse(parts[0]), int.Parse(parts[1]));
            }
            if (parts.Length >= 4)
            {
                sz = new Size(int.Parse(parts[2]), int.Parse(parts[3])); } } Size = sz; Location = il; }}Copy the code

Allow the user to reposition based on conditions: If the user holds down the SHIFT key while opening the application, it ignores the saved location and size information and displays it in the upper left corner with the default size specified in the designer. This allows users to override saved information if they plan to discard it completely!

The code is all about reading Settings, doing basic checks to make sure they work, and breaking them down into individual values. If there is a value for a position, the position is set. Size is also set if there is a value for size. There is no check in the code to make sure the values are integers – if not, your program is responsible because you saved the wrong information!

If you do not set a value on the Settings page, the basic check will fail, and the position and size will not change when the application first runs — it will appear in the upper left corner with the size set in the designer.

Save location and size

All that remains is to save the information when the program closes. Again, this is as simple as handling a FormClosing event:

private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
    if ((ModifierKeys & Keys.Shift) == 0)
    {
        Point location = Location;
        Size size = Size;
        if(WindowState ! = FormWindowState.Normal) { location = RestoreBounds.Location; size = RestoreBounds.Size; }string initLocation = string.Join(",", location.X, location.Y, size.Width, size.Height); Properties.Settings.Default.InitialLocation = initLocation; Properties.Settings.Default.Save(); }}Copy the code

Also, there is a way for users to skip, holding the SHIFT key while closing the application will not save information.

The FormClosing event is almost the right way to do it, because any click to close will trigger the FormClosing event. The only exception is that when minimizing, hide the display in the taskbar and put it in the task tray at the lower right corner. The software exit is realized by the right menu in the task tray, and the FormClosing will not be triggered. If you consider the most rigorous approach, consider the Application. The ApplicationExit events.

Pay attention to the point

That’s it. The only thing to keep in mind is that the application Settings files for the debug and release versions are different — so don’t expect the Settings to shift when you switch build types! (To be exact, whenever you change the path of the application, the Settings are lost. This is the default settings. Settings rule.)