Question save data to user.config that is not bound

MasonB

New member
Joined
Sep 23, 2009
Messages
3
Programming Experience
Beginner
Hello,
I would like to commit the data put in a checkedlistbox to the user.config. I have found a way to commit them to my.settings but no matter what Save() method I call, it does not commit the new data (that is not bound to a control, through the propertybindings) to the user.config, either immediately or when I shutdown the application. Which means, when I loop through the settings when the application starts back up, the data is not there to be added back to the checkedlistbox. I have put save methods in the formclosing events as well as when the data is created in the my.settings. using my.mysettings, my.settings and my.settings.default. Thank you in advance for your input.

This first method is a called when a button is pushed. it checks the text and then calls the next method (AddProperty) which should add the property to my.settings and save it to user.config, but my understanding is that it is not written to the file till the applicatoin closes.

VB.NET:
Private Sub btnErrorMessageAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnErrorMessageAdd.Click
        If txtAddError.Text <> "" And IsNothing(txtAddError.Text) = False Then
            chklstErrorMessages.Items.Add(txtAddError.Text, True)
        End If
        AddProperty(txtErrorName.Text, txtAddError.Text, GetType(String))
        'My.MySettings.Default.Reload()
        btnErrorMessageAdd.Visible = False
        txtAddError.Visible = False
        btnCancelAdd.Visible = False
        lblErrorName.Visible = False
        lblErrorMessage.Visible = False
        txtErrorName.Visible = False
        txtErrorName.Clear()
        txtAddError.Clear()
    End Sub
This method is the one that should add the property to my.settings (you can see by my commented out lines i tried it a couple different ways):

VB.NET:
Private Sub AddProperty(ByVal propertyName As String, _
          ByVal defaultValue As String, ByVal propertyType As Type)
        'Dim providerName As String = "LocalFileSettingsProvider"

        'Dim attributes As New Configuration.SettingsAttributeDictionary()
        'Dim attr As New Configuration.UserScopedSettingAttribute()
        'attributes.Add(attr.TypeId(), attr)

        'Dim prop As New Configuration.SettingsProperty( _
        ' New Configuration.SettingsProperty(propertyName, propertyType, _
        'My.MySettings.Default.Providers(providerName), False, defaultValue, _
        'Configuration.SettingsSerializeAs.String, attributes, False, False))

        'My.MySettings.Default.Properties.Add(prop)
        'My.Settings.Save()
        
        Dim p As Configuration.SettingsProperty

        p = New Configuration.SettingsProperty(propertyName)
        p.Provider = My.Settings.Providers("LocalFileSettingsProvider")
        p.DefaultValue = defaultValue
        p.SerializeAs = Configuration.SettingsSerializeAs.String
        p.Attributes.Add(GetType(Configuration.UserScopedSettingAttribute), _
                                   New Configuration.UserScopedSettingAttribute())
        p.PropertyType = GetType(String)
        My.Settings.Properties.Add(p)

        'Settings.Properties.Add(p)
        'My.Settings.PropertyValues.Add(New Configuration.SettingsPropertyValue(p))
        'My.Settings.Item(p.Name) = defaultValue


        My.Settings.Save()
    End Sub
Thank you.
 
To save My.Settings, check the "Save My.Settings on Shutdown" on the "Application" tab of the Visual Studio GUI.
 
That is checked already. I have a form that is wired up (propertybinding) to some values for settings database values. The values I want to add are not bound to anything since they would be added dynamically to a checkedlistbox.
 
You can't add settings at run time. You can only edit them. If you want to be able to add items to a list and then save those items to your settings then you need the list to be part of your setting to begin with. For that you need to add a StringCollection to your settings. You can then add edit and delete items in that StringCollection and the changes will be automatically saved at shutdown.
 
Back
Top