vb.netsettingsvb.net-2010

Creating form setting in VB Programmatically


My question is how to create a new form setting in vb.net language to save data Programmatically.

For example when i click button it will create the setting which it,s name is the text of the textbox1.

is this possible and how. And is there any functions which can save data when program is closed?


Solution

  • You can create your own settings-class which inherits ApplicationSettingsBase:

    Imports System.Configuration
    
    Public Class MyUserSettings
        Inherits ApplicationSettingsBase
        <UserScopedSetting()> _
        <DefaultSettingValue("white")> _
        Public Property BackgroundColor() As Color
            Get
                BackgroundColor = Me("BackgroundColor")
            End Get
    
            Set(ByVal value As Color)
                Me("BackgroundColor") = value
            End Set
        End Property
    End Class
    

    Save the settings:

    Dim Mus As New MyUserSettings
    Mus.BackgroundColor = Color.AliceBlue
    Mus.Save()
    

    Load the settings:

    Dim Mus As New MyUserSettings
    MessageBox.Show(Mus.BackgroundColor.ToString)
    

    Source: MSDN