Hello,
I was wondering if there is any way, I can have my Application save its settings upon exit or a save button to save to maybe a CFG File? such as textbox.text info, Checkbox.checked info, etc...

If there is could you please help?

Recommended Answers

All 3 Replies

See if this helps to save and load settings from a File.
2 CheckBoxes

Public Class Form1
    Private myCoolSettingsFile As String = "C:\test.CFG" '// your settings.File.

    '// use Region when needing to group together portions of code.  makes it easy to minimize the entire section and not one Sub at a time.
#Region "==========  SETTINGS ============"
    Private Sub saveSettings()
        Dim myCoolWriter As New IO.StreamWriter(myCoolSettingsFile)
        With myCoolWriter
            If CheckBox1.Checked Then .WriteLine("CheckBox1 is Checked") Else .WriteLine("CheckBox1 is NOT Checked")
            If CheckBox2.Checked Then .WriteLine("CheckBox2 is Checked") Else .WriteLine("CheckBox2 is NOT Checked")
            If CheckBox2.Checked Then .WriteLine("CheckBox2 is Checked") Else .WriteLine("CheckBox2 is NOT Checked")
        End With
        myCoolWriter.Close()
    End Sub
    Private Sub loadSettings()
        If IO.File.Exists(myCoolSettingsFile) Then
            Dim arTemp() As String = IO.File.ReadAllLines(myCoolSettingsFile) '// read all lines into arrays.
            Try '// not to crash on startup.
                If arTemp(0) = "CheckBox1 is Checked" Then CheckBox1.Checked = True '// check line 1.
                If arTemp(1) = "CheckBox2 is Checked" Then CheckBox2.Checked = True '// check line 2.
                If arTemp(2) = "CheckBox2 is Checked" Then CheckBox2.Checked = True '// check line 2.
                If arTemp(3) = "CheckBox2 is Checked" Then CheckBox2.Checked = True '// check line 2.
            Catch ex As Exception
                MsgBox("If you added new code to save and did not run the app. before you added code to load, you will get this message since the line you are checking for on startup has not been saved yet.", MsgBoxStyle.Information, "codeorder vb.net tip")
            End Try
        End If
    End Sub
#End Region

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        saveSettings()
    End Sub

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

Thanks So much! :)

:D, my apologies for the extra code lines for CheckBox2 and some incorrect comments for which line # of the file is being read.
Late night programming is not my specialty.

Glad I could help otherwise.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.