Saving Window position

Standard

When I use an application and I move the window to a new location on my monitor, or even move it over to the second monitor I expect that it would start up where I left it last. Very few application, to my annoyance, does this.

To build this into you applications is such an easy thing to do.

Bind you Location property to in the property binding window.

In the properties toolbox of your solution set a name for Location property.

Property Binding Dialog

This property will be stored in your app.config file.

 <userSettings>
    <xxx.UI.Properties.Settings>
      <setting name="ExportFolder" serializeAs="String">
        <value>Choose a folder...</value>
      </setting>
      <setting name="StartupPosition" serializeAs="String">
        <value>0, 0</value>
      </setting>
    </xxx.UI.Properties.Settings>
  </userSettings>

Next add code to you Window’s ResizeEnd event. This event is called once you stop moving a window too.

        private void MainWindow_ResizeEnd(object sender, EventArgs e)
        {
            Properties.Settings.Default.StartupPosition = Location;
        }

Now, in the constructor add the following event subscription.

// Save settings
Properties.Settings.Default.PropertyChanged += (o, e) => Properties.Settings.Default.Save();

Next time your window start’s up it will be in the location the user left it, even on the second monitor if it was last left there.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s