Software Development

Remember a form's position between uses in VB.NET

There are times when it's handy for developers to be able to remember where a form was located the last time the application was used. This quick VB.NET tip shows you a way of using Application Settings to store and use the last location of the form.

There are times when it's handy for developers to be able to remember where a form was located the last time the application was used. In this quick VB.NET tip, I'll show you a way of using Application Settings to store and use the last location of the form.

Using Application Settings

In order to store and use the form's location, I will use the Location property of the My.Settings object. Follow these steps to access the My.Settings object:

  1. Click the Project Properties and then click the Settings tab.
  2. In the Settings tab, set the Name to MainFormLocation and set the Type to System.Drawing.Point.
  3. Open the Form's properties and expand its Application Settings property. Find the Location property and set its value to MainFormLocation from the drop down list.

You are all set to use the application. Run it, then move the form, close, and re-run. The form should appear in the location where you last left it.

If you do the setup listed above, you will be able to programmatically access My.Settings.MainFormLocation (if necessary) since any settings added to the Settings tab of the Project Properties appear as the members of My.Settings object.

Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a project manager at Citigroup.

----------------------------------------------------------------------------------------

Get Visual Basic tips in your inbox

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically subscribe today!

6 comments
paul.james
paul.james

From a newbie- I understand what this is doing but I don't know how to code it. I'm using Framework 1.1/ VB 2003. Other tips mention writing the location & size info to an XML file. Does anyone have a code snippet I can use to better understand this? Thanks a Rube from Connecticut

Tony Hopkinson
Tony Hopkinson

and check to see if I've undocked ! and whether the resolution the coordinates were stored at has changed. From long suffering multi monitor user.

Justin James
Justin James

Using "My" and the settings for things like this is a real benefit, I have found! Just be careful of what you stick in there, make sure you encrypt any sensitive data. J.Ja

Justin James
Justin James

To be honest, it's been a while since I touched.Net 1.1, but even so, what you can do it store the settings in s DataSet, and then do DataSet.SaveTo XML (or something along those lines, I just woke up) which will save it. .Net 2.0 (and probably 1.1) also has an application settings system which handles this stuff automatically (saving settings), at the user & machine level. You'll want to look into that as well. J.Ja

john.minker
john.minker

Tony, How would I accomplish this? Thanks, John

Justin James
Justin James

I use Remote Desktop a lot, to access my home PC. Some apps remember the position on my dual monitor setup, so when I open them remotely, they are on the monitor that isn't there on the remote client. Bleh. J.Ja

Editor's Picks