Article: Windows Form Retain User Settings

Article Detail

Posted by:

 

Date:

Thursday, April 26, 2007 (CST)

 
Screen Shots



Article Brief Description

This article show you how you can retain the User setting like Size and position of a form. This article show you how it can retain the user form size and position when it exit the application.

Article Detail Description

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        SaveFormSettings()
End Sub
Windows Form Retain User Settings

This article show you how you can retain the User setting like Size and position of a form. This article show you how it can retain the  user form size and position when it exit the application. When the user opens the application again, the form is restored to the last user-defined position and size.

SettingsKey is the main feature that allows you to have multiple sets of settings.

In VS 2005 provides an inbuilt class that helps user preferences upon exit of the application and restore them when the user opens the application again.

This article shows you how you can retain the position and size of more that one form.

 

To define an application setting:

  • Open the Project Designer for the Windows Application project.
  • Select the Settings tab.
  • In the Name column, enter the name of the setting.
  • In the Type column, select the appropriate data type for the setting.
  • In the Scope column, define the scope: User or Application.

 

 

Private _frmSettings As My.MySettings

Private _frmSettings As My.MySettings
Private ReadOnly Property Settings() As _
System.Configuration.ApplicationSettingsBase
Get
    If _frmSettings Is Nothing Then
 _frmSettings = New My.MySettings
    End If
    Return _frmSettings
End Get
End Property

 

 

Private Sub ApplyFormSettings()
        Settings.SettingsKey = Me.Name
        Dim theSettings As My.MySettings
        theSettings = DirectCast(Settings, _
                                     My.MySettings)
        If theSettings.FormSize <> System.Drawing.Size.Empty Then
            Me.Size = theSettings.FormSize
        End If
        If theSettings.FormLocation <> Point.Empty Then
            Me.Location = theSettings.FormLocation
        End If
End Sub

 

 

Private Sub SaveFormSettings()
        Settings.SettingsKey = Me.Name
        Dim theSettings As My.MySettings
        theSettings = DirectCast(Settings, _
                                    My.MySettings)
        If Me.WindowState = FormWindowState.Normal Then
            theSettings.FormSize = Me.Size
        Else
            ' Return to the restore state based on
            ' form was maximized or minimized,
            
            theSettings.FormSize = Me.RestoreBounds.Size
        End If
        theSettings.FormLocation = Me.Location
        Settings.Save()
 End Sub

 

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

 

For Download a sample program click

http://www.coderschoice.com/Coders+Code+Bank/.NET+Windows+Form/2372.aspx


Average Rating:

Ferotech Solution Services Inc.