windowsvb.netwinformspreferencessavestate

Save Windows Form Size


I'm developing a piece in VB.NET. Inside my primary form, I'm creating a new form to use as a dialog. I was wondering if there was a way to, upon the close of the new dialog, save it's size settings for each user (probably in a file on their machine, through XML or something?)


Solution

  • you can save it to the settings file, and update it on the 'onclosing' event.

    to make a setting goto Project Properties ->settings -> then make a setting like 'dialogsize' of type system.drawing.size.

    then do this in your dialog form:

    Public Sub New()
        InitializeComponent()
    End Sub
    
    Public Sub New(ByVal userSize As Size)
        InitializeComponent()
        Me.Size = userSize
    End Sub
    
    Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
        MyBase.OnClosing(e)
        My.Settings.DialogSize = Me.Size
        My.Settings.Save()
    End Sub
    

    do something like this to check and use the setting:

        Dim dlg As MyDialogWindow
        If My.Settings.DialogSize.IsEmpty Then
            dlg = New MyDialogWindow()
        Else
            dlg = New MyDialogWindow(My.Settings.DialogSize)
        End If
        dlg.ShowDialog()