Results 1 to 3 of 3
  1. #1
    ♪~ ᕕ(ᐛ)ᕗ's Avatar
    Join Date
    Jun 2010
    Gender
    male
    Location
    Uterus
    Posts
    9,119
    Reputation
    1096
    Thanks
    1,970
    My Mood
    Doh

    [VB.NET]INI Tutorials 0

    0 is like the index. This is the first tut, I'm gonna make more if I find time...

    Well now I will show ya how to use a .ini (Configuration File) instead of the Settings. In this case to be honest Settings are better but since this is something that you should know I wanted to get this here.

    I'm gonna assume that u know the basics, else go learn them.

    Well start with making a normal form...Call it whatever u want.
    Add:
    1 TextBox
    1 Label
    1 Button

    We need a function to read and write into a specific line in the file...I'm gonna use:
    Code:
    To write:
        Public Sub ReplaceText(ByVal FileName As String, ByVal StartStr As String, ByVal ReplaceWith As String)
            Dim oldValue As String = ""
            Dim str As String = ""
            If File.Exists(FileName) Then
                Using reader As StreamReader = New StreamReader(FileName)
                    Do While Not reader.EndOfStream
                        oldValue = reader.ReadLine
                        If oldValue.Trim.ToLower.Contains(StartStr.Trim.ToLower) Then
                            oldValue = oldValue.Replace(oldValue, ReplaceWith)
                        End If
                        str = (str & oldValue & ChrW(13) & ChrW(10))
                    Loop
                End Using
                Using writer As StreamWriter = New StreamWriter(FileName, False)
                    writer.Write(str)
                End Using
            End If
        End Sub
    
    To read:
        Public Function ReadSpecifiedLine(ByVal file As String, ByVal lineNum As Integer) As String
            Dim contents As String = String.Empty
            Try
                Using stream As New StreamReader(file)
                    contents = stream.ReadToEnd().Replace(vbCr & vbLf, vbLf).Replace(vbLf & vbCr, vbLf)
                    Dim linesArray As String() = contents.Split(New Char() {ControlChars.Lf})
                    If linesArray.Length > 1 Then
                        If Not lineNum > linesArray.Length AndAlso Not lineNum < 0 Then
                            Return (linesArray(lineNum))
                        Else
                            Return (linesArray(0))
                        End If
                    Else
                        Return (contents)
                    End If
                End Using
            Catch ex As Exception
                Return (ex.ToString())
            End Try
        End Function
    Funcs are not mine, read the credits.

    Well now we need a .INI file in the start up path of the app. Make it and name it "settings.ini".

    Make the ini code:
    Code:
    Language = English
    ;The language of the program
    Create a new sub, variable and make it like this:
    Code:
    Public Lang As String = ReadSpecificLine(CurDir() & "\settings.ini", 0).Replace("Language = ", "") 'This variable will hold our current language name
    
    Public Sub GetLanguage()
        'easy if stat.
          If Lang = "English" Then _
                Label1.Text = "My name is Elio"
          If Lang = "Italian" Then Label1.Text = "Il mio nome e Elio" 'I can't write very good on italian and english too btw :p
    End Sub
    
    Public Sub SetLanguage()
         ReplaceText(CurDir() & "\settings.ini", "Language =", "Language = " & TextBox1.Text) 'Replaces the current language with an other
    
    'Recommended: Make a List/Array of strings which contains the supported languages to set, to make sure that while the run-time the app. will never crash.
    End Sub
    The functions are now done.

    Let's start with the Double-Clicking....

    D. Click on the form...
    Make the code...
    Code:
    GetLanguage()
    On the button...
    Code:
    SetLanguage()
    Hope you got the main idea of this

    Credits:
    Blubb1337 - Write Function
    Me - Main concept
    A dude from the WWW - Read function (Can't remember his name)

    Well enjoy =)

  2. #2
    Lyoto Machida's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Location
    Far away with girls
    Posts
    3,734
    Reputation
    133
    Thanks
    1,621
    My Mood
    Aggressive
    Good Job, Dead section is dead.

  3. #3
    ♪~ ᕕ(ᐛ)ᕗ's Avatar
    Join Date
    Jun 2010
    Gender
    male
    Location
    Uterus
    Posts
    9,119
    Reputation
    1096
    Thanks
    1,970
    My Mood
    Doh
    err yes pretty much