VSTO & .NET & Excel

March 19, 2010

Preserve Size and Location of Windows Forms – Part I

Filed under: .NET & Excel — Dennis M Wallentin @ 2:28 pm

Introduction
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.

The Case
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.

Kind regards,
Dennis

The second post in this mini series can be found here:
Preserve Size and Location of Windows Forms – Part II

About these ads

7 Comments »

  1. Very nice walkthrough, Dennis, works exactly as you explained it. :-)

    Doesn’t seem to work in C# though, for reasons I don’t understand. The App.Config and Applications Settings tab appear to be identical… And, yet, nothing happens. I wonder why…

    Comment by Mike Rosenblum — March 19, 2010 @ 3:24 pm

  2. Mike,

    That really surprise me so I created my own simple project in C#. It confirms what You say and I’m puzzled why it does not work.

    Thanks for pointing it out :)

    Kind regards,
    Dennis

    Comment by Dennis Wallentin — March 19, 2010 @ 4:35 pm

  3. I cant seem to find the answer online, so I’ve now posted this as a question on StackOverflow here:

    http://stackoverflow.com/questions/2478444/why-does-a-simple-applicationsetting-propertybinding-for-a-form-does-not-work-in

    I’m sure that the experts there should be able to straighten this out. I suspect that something very minor is missing…

    Comment by Mike Rosenblum — March 19, 2010 @ 5:27 pm

  4. Ok, based on the help given on StackOverflow, and comparing the code in the VB.NET and C# assemblies, it seems that VB.NET automatically saves the settings by calling ‘My.Settings.Save()’ from within the ‘MySettings.AutoSaveSettings()’ method according to the ‘My.Application.SaveMySettingsOnExit’ value, which must default to being ‘True’.

    It appears that C# does not automatically save these settings for us, so we must save them explicitly ourselves by calling ‘Properties.Settings.Default.Save()’ from within the ‘FormClosed’ event for the form.

    That’s it! It seems that VB.NET is still a little easier with respect to RAD, while C# requires the coder to be a bit more explicit.

    Comment by Mike Rosenblum — March 19, 2010 @ 6:51 pm

  5. Mike,
    Thanks a lot for the clarification and it’s big news for me that C# does not automatically call the Save() function.

    Yes, again it turn out that C# is a more “plumber” tool than VB.

    Thanks,
    Dennis

    Comment by Dennis Wallentin — March 19, 2010 @ 7:35 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Silver is the New Black Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 36 other followers

%d bloggers like this: