Enabling a Windows Presentation Foundation (WPF) App to Open in Its Last Window Location | CodeGuru

Enabling a Windows Presentation Foundation (WPF) App to Open in Its Last Window Location

In this tutorial you are going to learn how to get your Microsoft WPF (Windows Presentation Foundation) application to open in its last location before it was closed. You are going to do this using VB.NET code and application settings. Create a new WPF application project. Ensure that it is a Visual Basic project type. […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 28, 2010
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

In this tutorial you are going to learn how to get your
Microsoft WPF (Windows Presentation
Foundation)
application to open in its last location
before it was closed. You are going to do this using VB.NET code and application
settings.

  1. Create a new WPF application project. Ensure that it is
    a Visual Basic project type. Name it ‘Location’.
  2. Once your project is created place a Label control in
    the window and change its Content property to “I was Here”.
  3. Change the TextSize property of the label in the Text
    section of the properties window to 20. Resize the label if
    you need to to make the text visible and drag the label
    until it is at about the center of the window.
  4. Select Window1 and in the CommonProperties
    section of the Properties window ensure that the
    WindowStartupLocation is set to Manual.
  5. Right click on the Location project in the Solution
    Explorer and select properties from the context menu or
    double click the MyProject file in the project
    tree. This opens the properties window.
  6. Select the Settings tab. Notice that there is a default
    setting that is already present. Right click inside the
    first cell and select ‘Add Setting’ from the context menu.
    The default text is selected. Type in the text
    LastLeft‘.
  7. Select double from the Type dropdown list and leave the
    Scope as user. Leave the Value cell as is.
  8. In the second row type ‘LastTop‘ in the
    Name section and select a Type of Double. Leave the Scope
    and Value section as default. Save your work.
  9. Now you are going to add some code to make use of the
    previously created settings. We will be using the
    My.Settings keyword and the Left and Top window properties
    which get or set the position of the window’s left and top
    edges in relation to the desktop.

  10. Double click on Window1.xaml.vb in the project tree to
    open the code behind file.
  11. Select ‘Window1 Events’ from the ClassName combo box and the
    Closing event from the MethodName combo box.
  12. In the Window1_Closing event procedure type the
    following code;

  13.      With My.Settings
    	.LastLeft = Me.Left
    	.LastTop = Me.Top
    	.Save()
         End With
    

    Your Window1 class should now look like this;


      	  Class Window1
          		Private Sub Window1_Closing(ByVal sender As Object, _
      	 	  ByVal e As System.ComponentModel.CancelEventArgs) _
      	 	  Handles Me.Closing
             		With My.Settings
                 		    .LastLeft = Me.Left
                  	            .LastTop = Me.Top
                  	            .Save()
              		End With
          		End Sub
      	   End Class
    

  14. Select Window1 Events from the ClassName combo box and the
    Loaded event from the MethodName combo box. Type in the
    following code in the Window1_Loaded event
    procedure.

  15. Me.Left = My.Settings.LastLeft
    Me.Top = My.Settings.LastTop
    

    Click on the Save All button or press Ctrl+Shift+S

  16. Run the application by pressing F5 or by clicking on the
    Start Debugging button.
  17. Drag the window to a different part of your screen and
    close it.
  18. Run the application again by pressing the F5 key. Notice
    that the window opens in the last location where you closed
    it.

Using My.Settings.Save ( ) allows us to persist the application settings when Window1 is closed. This ensures that the window will open in the last location where it was closed since we set the Left and Top properties of Window1 to the LastLeft and LastTop settings in the Window1_Loaded event procedure.

Thanks for reading!

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.