Results 1 to 5 of 5
  1. #1
    edub18's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Location
    Germany
    Posts
    146
    Reputation
    10
    Thanks
    12
    My Mood
    Bored

    [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

  2. #2
    justiman's Avatar
    Join Date
    Nov 2010
    Gender
    male
    Posts
    47
    Reputation
    10
    Thanks
    4
    My Mood
    Fine
    nice good job

  3. #3
    wezljkz's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Posts
    23
    Reputation
    10
    Thanks
    0
    good job , was need this .
    do more tutorials :] .

    and if you can , please help me here:
    https://www.mpgh.net/forum/33-visual-...omethings.html

  4. #4
    VernK's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    49
    Reputation
    10
    Thanks
    47
    My Mood
    Relaxed
    Nice tutorial this would be usefull to the beginners
    I am Here to Program!
    __________________________________________________
    Open Source Project: [Advanced IP Tool][Released]
    Open Source Project: [Advanced IP Tracker][Released]
    Download the project here! - IP Tool v1.5 By VernK - Full Source
    __________________________________________________
    Visual Basic: 67%
    Visual C#: 23%
    Visual Web Dev: 10%
    Visual C++: 5%


    Feel free to ask me any questions or get help:
    Skype: veeran.kerai. rocks (Active) <remove space>
    AIM: veerank12@aol.com
    YAHOO: veerank12@yahoo.com
    MSN: veerank12@live.com (Very Active)
    GMAIL: movemanb@gmail.com
    E-MAILS: veerank12@mail.com
    veerank12@gm*****m
    Or PM Me on MPGH!

  5. #5
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    You may as well try this too. Works without win API:

    https://www.mpgh.net/forum/33-visual-...ml#post2549559

    ;D