To preserve size and location of Windows Forms are in many ways a common service towards the end users. In this first part I will take a closer look into how we can remember theses settings between the sessions for standalone applications in VB.NET. In part two and last article I will discuss it for managed COM add-ins and VSTO add-ins.
First, create a new Windows Form application in VS and we start with the solution for the location of the Windows Form. Actually, it is quite easy to set it up; make sure that the Windows Form is selected and in its properties windows select (PropertyBinding) as the following screen shot shows:
Next, select the button … to the right and in the next window select the property Location as the next screen shot shows:
Select (New) in the dialog so that the following dialog is showed:
Name the new Application setting to Location and then click on the OK button. That’s it! To set up the size setting we repeat the above steps but instead of selecting the property Location we select the ClientSize and name it to Size. So whenever the end users change the Windows Form’s location and size the above solution will automatically preserve these two properties.
When we have made the set up and when viewing the Windows Form’s property window we now see two new entries which the screen shot show below also shows:
When selecting the Solution Explore we also note that a new file has been added to the project - app.config.xml – which the following screen shot also reveals:
It is this XML file the changes are being saved into. If we open the XML file we can see the following entries which hold the necessary data:
<userSettings> <WindowsSizeLocation.My.MySettings> <setting name="Location" serializeAs="String"> <value>0, 19</value> </setting> <setting name="Size" serializeAs="String"> <value>325, 150</value> </setting> </WindowsSizeLocation.My.MySettings> </userSettings>
Actually, the same information can also be reviewed from the project’s property window as the following screen shot also shows:
In view of the above we can conclude that is quite easy to keep track when size and location are being changed for any Windows Form.
The second post in this mini series can be found here:
Preserve Size and Location of Windows Forms – Part II