[Tutorial][VB2008] Saving your program's settings

Posts 1–5 of 5 · Page 1 of 1
[Tutorial][VB2008] Saving your program's settings
Heyho,
in this tutorial, im going to show you, how to save and load your programs title, a text box (txtText) and the state of 2 CheckBoxes (chkBox1 & chkBox2) in an Inifile.

What is an Inifile?
An Inifile is a file where you can store information. It old but very easy.

Structure:
[Section]
Key=Value

Needed controls:
1 Textbox (txtText)
2 Checkboxes (chkBox1 & chkBox2)
1 Button (cmdSave)

The code:
First you need a class to handle the Inifiles. Just right click on your project and choose -> Add Class. Name the Class "cINIFiles.vb" and paste the code shown below.

Code:
Option Explicit On

Public Class cINIFiles
    Private Declare Ansi Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" ( _
        ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, _
        ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer

    Private Declare Ansi Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
        ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, _
        ByVal lpFileName As String) As Integer

    Public Path As String

    Public Function ReadValue(ByVal Section As String, ByVal Key As String, Optional ByVal DefaultString As String = "", Optional ByVal BufferSize As Integer = 1024) As String
        If Path = "" Then
            MsgBox("No path was specified", MsgBoxStyle.Exclamation, "Error")
            ReadValue = "Readerror"
            Exit Function
        End If

        If IO.File.Exists(Path) = False Then
            MsgBox("This file does not exists", MsgBoxStyle.Exclamation, "Error")
            ReadValue = "Readerror"
            Exit Function
        End If

        Dim sTemp As String = Space(BufferSize)
        Dim Length As Integer = GetPrivateProfileString(Section, Key, DefaultString, sTemp, BufferSize, Path)
        Return Left(sTemp, Length)
    End Function

    Public Sub WriteValue(ByVal Section As String, ByVal Key As String, ByVal Value As String)
        If Path = "" Then
            MsgBox("No path was specified", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If

        Dim Ordner As String
        Ordner = IO.Path.GetDirectoryName(Path)
        If IO.Directory.Exists(Ordner) = False Then
            MsgBox("This file does not exist", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If

        WritePrivateProfileString(Section, Key, Value, Path)
    End Sub
End Class
Now for the main form. First of all, you have to 'Dim' a variable as cINIFile. You do that right under "Public Class YOURFORMNAME".

Code:
Public Class frmMain
    Dim inifile As New cINIFiles
Next, we're going to use 2 functions (save & load). Just paste this at the very bottom of your code (right before "End Class").

Code:
    Private Sub SaveInformation()
        inifile.WriteValue("General", "Title", Me.Text) 'Write the form "title" to section "General"
        inifile.WriteValue("General", "Text", txtText.Text)
        inifile.WriteValue("General", "Box1", chkBox1.Checked)
        inifile.WriteValue("General", "Box2", chkBox2.Checked)
    End Sub
Code:
    Private Sub LoadInformation()
        Me.Text = inifile.ReadValue("General", "Title", "") 'Read the form "title" of the section "General"
        txtText.Text = inifile.ReadValue("General", "Text", "")
        chkBox1.Checked = inifile.ReadValue("General", "Box1", "")
        chkBox2.Checked = inifile.ReadValue("General", "Box2", "")
    End Sub
Now, inside the "Form Load"-Event, we want to load the information from the Inifile. We do that by calling the "LoadInformation"-Function. But first, we have to specify the path of the Inifile. In this case, its in the same folder as my program and its called "settings.ini".

Code:
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        inifile.Path = Application.StartupPath & "\settings.ini" 'Set the path to your INI-File

        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\settings.ini") = True Then 'Check if the file exists
            LoadInformation() 'Call the function to load
        End If
Its that simple!
Now to save it.

Code:
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        SaveInformation() 'Call the function to save
    End Sub
And thats it!
If you want to save additional information, just change the functions "SaveInformation()" & "LoadInformation()".

Greets
nice good job
Nice tutorial this would be usefull to the beginners
Posts 1–5 of 5 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

None

Talk with us