using applicationsettings/propertybinding to save form size etc.

I want to be able to save the following:

The last position and size of the form when not maximized and the last windowstate. ie if the user closed the app maximized, next time I want it to open maximized, but if the user sets it back to a normal window, I want it to display using the save size and location (as Microsoft Word does).

I have created three user settings with the appropriate type. If I bind these to the form properties, it seems to really screw up (it works ok until I add the windowstate, but I need this setting. Otherwise, if I maximize the form and then close and reload the app, the windowstate is set to normal, but the size is set to the whole screen).

I have also tried setting these properties in the constructor and changing the settings on the locationchanged and resizeend events as there does not seem to be a windowstatechanged event. This saves the size and window state OK, but the position never seems to work correctly.


public frmMain()

{

InitializeComponent();

this
.Location = Properties.Settings.Default.MainFormLocation;

this.Size = Properties.Settings.Default.MainFormSize;

this.WindowState = Properties.Settings.Default.MainFormWindowState;

}

private void frmMain_LocationChanged(object sender, EventArgs e)

{

if (this.WindowState == FormWindowState.Normal)

Properties.Settings.Default.MainFormLocation = this.Location;

else

 

Properties.Settings.Default.MainFormWindowState = this.WindowState;

}

private void frmMain_ResizeEnd(object sender, EventArgs e)

{

if (this.WindowState == FormWindowState.Normal)

Properties.Settings.Default.MainFormSize = this.Size;

else

 

Properties.Settings.Default.MainFormWindowState = this.WindowState;

}


 



What is the best way to achieve my requirements

Thanks.



Answer this question

using applicationsettings/propertybinding to save form size etc.

  • Ryo44453

    Click the Project | ProjectXXX Properties Menu, click the Settings tab and change the third column (Scope) from Application to User for each setting you want to be able to save on a per user basis.
  • Keith Verity

    the property settings must have user scope to be saved. Application properties are read-only.
  • eb1959

    Hello,

    When I tried the following in VB.Net I got an error because the Forms.PropertyStore is private. Any ideas and/or suggestions Thanks!

    Private
    Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me
    .ClientSize = Properties.Settings.Default.MySize
        Me.Location = Properties.Settings.Default.MyLoc
        Me.WindowState = Properties.Settings.Default.MyState

    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

        Properties.Settings.Default.MyState =
    Me.WindowState
        If Me.WindowState = FormWindowState.Normal Then
            Properties.Settings.Default.MySize = Me.Size
            Properties.Settings.Default.MyLoc =
    Me.Location
        Else
            Properties.Settings.Default.MySize = Me.RestoreBounds.Size
            Properties.Settings.Default.MyLoc =
    Me.RestoreBounds.Location
        End If
        Properties.Settings.Default.Save()

    End Sub


  • Vance

    You can achieve this using the Form.RestoreBounds property. Here is some code I mocked up that seems to do what you want:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {

            Properties.
    Settings.Default.MyState = this.WindowState;

    if (this.WindowState == FormWindowState.Normal)

    {

    Properties.Settings.Default.MySize = this.Size;

    Properties.Settings.Default.MyLoc = this.Location;

    }

    else

    {

    Properties.Settings.Default.MySize = this.RestoreBounds.Size;

    Properties.Settings.Default.MyLoc = this.RestoreBounds.Location;

    }

    Properties.Settings.Default.Save();

    }

    private void Form1_Load(object sender, EventArgs e)

    {

    this.Size = Properties.Settings.Default.MySize;

    this.Location = Properties.Settings.Default.MyLoc;

    this.WindowState = Properties.Settings.Default.MyState;

    }


  • mikey4865

    The explanation for this error is probably due to a missing imports statement:

    Properties is the name of an internal property of the class Control (which is a parent of Form) returning an internal class (System.Windows.Forms.PropertyStore). Therefore, the compiler thinks you are trying to access this property (instead of your application settings). This is why the error message seems weird.

  • devlindark

    This certainly seems buggy.  Log a bug with the Product Feeback center and you can track it. In the meantime, it works if you manually use the settings...



    private void Form1_Load(object sender, EventArgs e)

    {

    this.WindowState = Properties.Settings.Default.Form1WindowState;

    this.Location = Properties.Settings.Default.Form1Location;

    this.Size = Properties.Settings.Default.Form1Size;

    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)

    {

    Properties.Settings.Default.Form1Location = this.Location;

    Properties.Settings.Default.Form1Size = this.Size;

    Properties.Settings.Default.Form1WindowState = this.WindowState;

    Properties.Settings.Default.Save();

    }



     



  • Mikey77

    Thanks Zaph0d! That makes sense. How do I get to a user scope as this is all new to me
  • rileyt

    I have been in a similar situation but using VB 2005 express.

    When a form is minimized its location property becomes (-32000,-32000) and its size property that which it occupies on the 'bar'. To get round this MS cleverly invented the RestoreProperty ... but....... the restore property seems to hold the form's size correctly but always appears to have the location as (0,0).

    As I understand the documentation it should hold the bounds -- location AND size -- while the form's windowstate is not 'normal' (but not when it is normal).

    Of course I can get round this situation by holding both size and location myself and updating in the LocationChanged and SizeChanged event procedures but this is re-inventing the wheel that MS documented but apparently has not quite implemented.

    Any comments


  • using applicationsettings/propertybinding to save form size etc.