0

Hope someone can help. I want to load the Sections of an external ini file into a ComboBox in Visual Basic 2015 and then load the Keys and Values of a selected Section into TextBoxes. I've been pulling my hair out with this and can't even get a start on how to accomplish this.

Any helping start you can give would be greatly appreciated.

Thanks, Dan

  • See [this](http://www.codeproject.com/Articles/1966/An-INI-file-handling-class-using-C). – Fᴀʀʜᴀɴ Aɴᴀᴍ Oct 25 '15 at 09:43
  • That's the problem right there. I have tried all different types of stuff that I am finding online and none of them work. For example, I have tried this - [link](http://stackoverflow.com/questions/30028304/populate-combo-box-with-ini-file-in-vb-net) But to not avail. – user3719039 Oct 25 '15 at 09:43
  • try the one i suggested. it works. i have tested it myself. – Fᴀʀʜᴀɴ Aɴᴀᴍ Oct 25 '15 at 09:44
  • "and then load the Keys and Values of a selection into TextBoxes" - is the ini file always constant? – Fᴀʀʜᴀɴ Aɴᴀᴍ Oct 25 '15 at 10:12
  • @FarhanAnam You sir are a legend. Thank you so much. That filles the ComboBox with the Sections. Now I just need to figure out how to populate text boxes with the Key and Values by selecting a Section. Thank you so much. :) – user3719039 Oct 25 '15 at 10:52
  • see my answer below. It has a better class which can provide a list of all sections as well as the value of a key in a section. – Fᴀʀʜᴀɴ Aɴᴀᴍ Oct 25 '15 at 10:54

1 Answers1

1

First create this class(Source - CodeProject Article):

Imports System.Text
Imports System.Runtime.InteropServices
Public Class INI

    <DllImport("kernel32")> _
    Private Shared Function GetPrivateProfileString(Section As String, Key As String, Value As String, Result As StringBuilder, Size As Integer, FileName As String) As Integer
    End Function


    <DllImport("kernel32")> _
    Private Shared Function GetPrivateProfileString(Section As String, Key As Integer, Value As String, <MarshalAs(UnmanagedType.LPArray)> Result As Byte(), Size As Integer, FileName As String) As Integer
    End Function

    <DllImport("kernel32")> _
    Private Shared Function GetPrivateProfileString(Section As Integer, Key As String, Value As String, <MarshalAs(UnmanagedType.LPArray)> Result As Byte(), Size As Integer, FileName As String) As Integer
    End Function

    Public path As String
    Public Sub New(INIPath As String)
        path = INIPath
    End Sub

    Public Function GetSectionNames() As String()
        Dim maxsize As Integer = 500
        While True
            Dim bytes As Byte() = New Byte(maxsize - 1) {}
            Dim size As Integer = GetPrivateProfileString(0, "", "", bytes, maxsize, path)
            If size < maxsize - 2 Then
                Dim Selected As String = Encoding.ASCII.GetString(bytes, 0, size - (If(size > 0, 1, 0)))
                Return Selected.Split(New Char() {ControlChars.NullChar})
            End If
            maxsize *= 2
        End While
    End Function
    Public Function GetEntryNames(section As String) As String()
        Dim maxsize As Integer = 500
        While True
            Dim bytes As Byte() = New Byte(maxsize - 1) {}
            Dim size As Integer = GetPrivateProfileString(section, 0, "", bytes, maxsize, path)
            If size < maxsize - 2 Then
                Dim entries As String = Encoding.ASCII.GetString(bytes, 0, size - (If(size > 0, 1, 0)))
                Return entries.Split(New Char() {ControlChars.NullChar})
            End If
            maxsize *= 2
        End While
    End Function
    Public Function GetEntryValue(section As String, entry As String) As Object
        Dim maxsize As Integer = 250
        While True
            Dim result As New StringBuilder(maxsize)
            Dim size As Integer = GetPrivateProfileString(section, entry, "", result, maxsize, path)
            If size < maxsize - 1 Then
                Return result.ToString()
            End If
            maxsize *= 2
        End While
    End Function
End Class

Add sections to the ComboBox like this:

Dim _ini As New INI("somefile.ini")
ComboBox1.Items.AddRange(ini.GetSectionNames()) 'For all sections
ComboBox1.Items.Add("section1") 'For specific section(s)

Then, in the selection changed event of the ComboBox:

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim _ini As New INI("somefile.ini")
    Dim section As String = ComboBox1.SelectedItem
    TextBox1.Text = _ini.GetEntryValue(section, "someKey") 'for specific entry
    For Each item In _ini.GetEntryNames(section) 'this is for all entries
        'do whatever you want here with the item variable like this:
        'TextBox1.Text = _ini.GetEntryValue(section, item)...
    Next
End Sub
Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
  • Can I ask another question? I'm really sorry. I have the Sections displayed in a ComboBox and when I click one it shows the Keys in a ListBox. That all works fine. But I am having trouble adding the Value of the key to a TextBox when a Key is selected. Any idea? – user3719039 Oct 25 '15 at 12:35
  • in the `ListBox.SelectedIndexChanged` or maybe a Button click, create another instance of ini like this : `Dim _ini As New INI("somefile.ini")`. Since you are accessing the ini multiple times, you may even consider declaring a single instance of the INI at a class level. Then, use `TextBox1.Text = _ini.GetEntryValue(ComboBox1.SelectedItem, ListBox1.SelectedItem)`. **You can use some conditions to this code to execute only when something is selected otherwise it will throw a Null Reference Exception. – Fᴀʀʜᴀɴ Aɴᴀᴍ Oct 25 '15 at 16:25