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

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds