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.

  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!



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The first phase of API management was about realizing the business value of APIs. This next wave of API management enables the hyper-connected enterprise to drive and scale their businesses as API models become more complex and sophisticated. Today, real world product launches begin with an API program and strategy in mind. This API-first approach to development will only continue to increase, driven by an increasingly interconnected web of devices, organizations, and people. To support this rapid growth, …

  • 10 Rules that Make or Break Enterprise App Development Projects In today's app-driven world, application development is a top priority. Even so, 68% of enterprise application delivery projects fail. Designing and building applications that pay for themselves and adapt to future needs is incredibly difficult. Executing one successful project is lucky, but making it a repeatable process and strategic advantage? That's where the money is. With help from our most experienced project leads and software engineers, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds