My.Settings and form inheritance

Posted Tuesday, January 17, 2006 3:25 PM by Maurice

The My.Settings feature in Visual Basic 2005 is a great way to store form settings like the forms location so the user gets the form at the same place as he/she closed it last time. Adding it is real simple. Just go to ApplicationSettings in the property sheet, expand it, open the combo after location, choose new and type a name like “FormLocation”. Simple as that and you are done [:)]

 However there are some problems [:(]

 The property name must be unique, Give two separate forms the same name and both will be shown at exactly the same position. Makes it kind of hard to view the info on both [:(]

 Adding the ApplicationSettings to a base form class means that all derived forms use the same property name.

 However there is an easy way around it. By using the My.Settins.SettingsKey you can group related entries together. Adding the following code to you base form class will ensure that all form classes keep their own distinct location:

Public Class BaseForm
    Private _setting As New My.MySettings() 

    Private Sub BaseForm_FormClosed( _
        ByVal sender As Object, _
        ByVal e As System.Windows.Forms.FormClosedEventArgs) _
        Handles Me.FormClosed

         _setting.Save()
    End Sub 

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

        _setting.SettingsKey = Me.GetType().FullName
        DataBindings.Add(New Binding("Location", _
            _setting, _
            "FormLocation", _
            True, _
            DataSourceUpdateMode.OnPropertyChanged))
    End Sub
End
Class

 
Maurice de Beijer
www.TheProblemSolver.nl

 

Leave a Comment

(required) 
(required) 
(optional)
(required)