.Net Custom Configuration Section and Saving Changes within PropertyGrid

Posted by Paul on Stack Overflow See other posts from Stack Overflow or by Paul
Published on 2010-03-17T16:59:21Z Indexed on 2010/03/17 17:01 UTC
Read the original article Hit count: 618

If I load the My.Settings object (app.config) into a PropertyGrid, I am able to edit the property inside the propertygrid and the change is automatically saved.

PropertyGrid1.SelectedObject = My.Settings

I want to do the same with a Custom Configuration Section.

Following this code example (from here http://www.codeproject.com/KB/vb/SerializePropertyGrid.aspx), he is doing explicit serialization to disk when a "Save" button is pushed.

  Public Class Form1
   'Load AppSettings
  Dim _appSettings As New AppSettings()

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    _appSettings = AppSettings.Load()
    ' Actually change the form size
    Me.Size = _appSettings.WindowSize
    PropertyGrid1.SelectedObject = _appSettings
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    _appSettings.Save()
  End Sub
End Class

In my code, my custom section Inherits from ConfigurationSection (see below) Question: Is there something built into ConfigurationSection class that does the autosave? If not, what is the best way to handle this, should it be in the PropertyGrid.PropertyValueChagned? (how does the My.Settings handle this internally?)

Here is the example Custom Class that I am trying to get to auto-save and how I load into property grid.

   Dim config As System.Configuration.Configuration = _
   ConfigurationManager.OpenExeConfiguration( _
   ConfigurationUserLevel.None)

    PropertyGrid2.SelectedObject = config.GetSection("CustomSection")





Public NotInheritable Class CustomSection
    Inherits ConfigurationSection
    ' The collection (property bag) that contains 
    ' the section properties.
    Private Shared _Properties As ConfigurationPropertyCollection


    ' The FileName property.
    Private Shared _FileName As New ConfigurationProperty("fileName", GetType(String), "def.txt", ConfigurationPropertyOptions.IsRequired)
    ' The MasUsers property.
    Private Shared _MaxUsers _
    As New ConfigurationProperty("maxUsers", _
    GetType(Int32), 1000, _
    ConfigurationPropertyOptions.None)

    ' The MaxIdleTime property.
    Private Shared _MaxIdleTime _
    As New ConfigurationProperty("maxIdleTime", _
    GetType(TimeSpan), TimeSpan.FromMinutes(5), _
    ConfigurationPropertyOptions.IsRequired)


    ' CustomSection constructor.
    Public Sub New()

        _Properties = New ConfigurationPropertyCollection()
        _Properties.Add(_FileName)
        _Properties.Add(_MaxUsers)
        _Properties.Add(_MaxIdleTime)


    End Sub 'New


    ' This is a key customization. 
    ' It returns the initialized property bag.
    Protected Overrides ReadOnly Property Properties() _
    As ConfigurationPropertyCollection
        Get
            Return _Properties
        End Get
    End Property


    <StringValidator( _
    InvalidCharacters:=" ~!@#$%^&*()[]{}/;'""|\", _
    MinLength:=1, MaxLength:=60)> _
    <EditorAttribute(GetType(System.Windows.Forms.Design.FileNameEditor), GetType(System.Drawing.Design.UITypeEditor))> _
    Public Property FileName() As String
        Get
            Return CStr(Me("fileName"))
        End Get
        Set(ByVal value As String)

            Me("fileName") = value
        End Set
    End Property


    <LongValidator(MinValue:=1, _
    MaxValue:=1000000, ExcludeRange:=False)> _
    Public Property MaxUsers() As Int32
        Get
            Return Fix(Me("maxUsers"))
        End Get
        Set(ByVal value As Int32)
            Me("maxUsers") = value
        End Set
    End Property



    <TimeSpanValidator(MinValueString:="0:0:30", _
    MaxValueString:="5:00:0", ExcludeRange:=False)> _
    Public Property MaxIdleTime() As TimeSpan
        Get
            Return CType(Me("maxIdleTime"), TimeSpan)
        End Get
        Set(ByVal value As TimeSpan)
            Me("maxIdleTime") = value
        End Set
    End Property
End Class 'CustomSection  

© Stack Overflow or respective owner

Related posts about custom-sections

Related posts about app.config