views:

224

answers:

1

My question involves this simple walkthrough shown in the article Preserve Size and Location of Windows Forms – Part I by Dennis Wallentin.

This approach works 100% fine when using VB.NET. When using the same steps in C#, however, the settings within the Settings tab of the application's properties looks correct, and the app.config file looks right, but the values are not saved when you run it.

The app.config file winds up looking like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="WindowsAppCs.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <WindowsAppCs.Properties.Settings>
            <setting name="Location" serializeAs="String">
                <value>0, 0</value>
            </setting>
            <setting name="Size" serializeAs="String">
                <value>284, 262</value>
            </setting>
        </WindowsAppCs.Properties.Settings>
    </userSettings>
</configuration>

It looks right to me, but the values do not update when running hosted within Visual Studio or when running the compiled EXE.

I am sure that something very simple needs to be added or done, but I don't know what. Does anyone have any ideas here?

Much thanks in advance...

+1  A: 

Are you saving the settings after the form has been resized? Doesn't Windows already remember the last position and size of your form?

Icono123
(1) "Are you saving the settings after the form has been resized". I've done nothing more than shown in that walkthrough. I'm guessing that the VB.NET designer must be adding code to automatically save the settings, but the C# designer is not... But I don't know what I'm supposed to add. (2) "Doesn't Windows already remember the last position and size of your form?". No, you have to save the size and position yourself if you wish to achieve this functionality.
Mike Rosenblum
Try adding a form resize event and call this in it: Properties.Settings.Default.Save();
Icono123
I found this online: http://www.dotnetspider.com/resources/22181-Restore-Form-Size-Position-C.aspx.
Icono123
Ok, thanks Icono, that seems to explain it. It seems that VB.NET automatically calls My.Settings.Save() upon exit, but C# does not. So we have to add `Properties.Settings.Default.Save()` ourselves from within the `FormClosed` event. Thanks for the help!
Mike Rosenblum
Yup, the WindowsFormsApplicationBase class does this. Quite usable from a C# program as well: http://stackoverflow.com/questions/392864/c-splash-screen-problem
Hans Passant