Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: VB 2010 how to populate a listbox from an ini file

05 Feb 2015   #1
JBmorris

Windows Vista Business 32bit
 
 
VB 2010 how to populate a listbox from an ini file

Hello SF users! Okay so I am making a program that involves listboxes and ini files. What I am trying to do is populate the listbox from an ini file(make the listbox show strings from a section). I don't have any code yet so... How do I do this?


My System SpecsSystem Spec
.
05 Feb 2015   #2
Tookeri

Windows 7 Pro 32
 
 

I've never read an entire section, but this solution looks ok:
vb.net - Read sections of INI file in VB .NET 2012 - Stack Overflow

Then where this line is, you add the info to the listbox:
' do something with the information here
My System SpecsSystem Spec
05 Feb 2015   #3
Tookeri

Windows 7 Pro 32
 
 

Sorry, I missed the fact that that code didn't look up a specific section. So forget that and try this instead:

Code:
Dim inifile As String = System.AppDomain.CurrentDomain.BaseDirectory() & Replace(LCase(IO.Path.GetFileName(Application.ExecutablePath)), ".exe", ".ini")
Dim objReader As New System.IO.StreamReader(inifile, System.Text.Encoding.Default)
Dim row As String
Dim section As String = "Links"
Dim match As Boolean = False
Do While objReader.Peek() <> -1
    row = objReader.ReadLine()
    If LCase(row) = "[" & LCase(section) & "]" Then
        match = True
    ElseIf match AndAlso Trim(row) <> "" AndAlso Mid(row, 1, 1) <> "[" Then
        Dim data() As String = Split(row, "=")
        Dim key = data(0)
        Dim value = data(1)
        Debug.Print(key & "=" & value)
        'listbox.Items.Add(key & "=" & value)
    Else
        match = False
    End If
Loop
objReader.Close()
Change the value for inifile to the path and filename to it. The value for it above is something I use to avoid having hard coded paths and file names. It takes the current directory and the name of the exe and changes exe to ini. When you run the program from VB it means the bin folder in your project folder. So it looks for an ini file with the same name as the exe file, in the "current directory".
Then set the value for section. I've added a commented example of how to add the key and value to a listbox.

The code will read line by line looking for the section. After it finds it it will read the following key/value lines until it reaches either an empty line or a new section - row value beginning with [
My System SpecsSystem Spec
.

05 Feb 2015   #4
JBmorris

Windows Vista Business 32bit
 
 

Quote   Quote: Originally Posted by Tookeri View Post
Sorry, I missed the fact that that code didn't look up a specific section. So forget that and try this instead:

Code:
Dim inifile As String = System.AppDomain.CurrentDomain.BaseDirectory() & Replace(LCase(IO.Path.GetFileName(Application.ExecutablePath)), ".exe", ".ini")
Dim objReader As New System.IO.StreamReader(inifile, System.Text.Encoding.Default)
Dim row As String
Dim section As String = "Links"
Dim match As Boolean = False
Do While objReader.Peek() <> -1
    row = objReader.ReadLine()
    If LCase(row) = "[" & LCase(section) & "]" Then
        match = True
    ElseIf match AndAlso Trim(row) <> "" AndAlso Mid(row, 1, 1) <> "[" Then
        Dim data() As String = Split(row, "=")
        Dim key = data(0)
        Dim value = data(1)
        Debug.Print(key & "=" & value)
        'listbox.Items.Add(key & "=" & value)
    Else
        match = False
    End If
Loop
objReader.Close()
Change the value for inifile to the path and filename to it. The value for it above is something I use to avoid having hard coded paths and file names. It takes the current directory and the name of the exe and changes exe to ini. When you run the program from VB it means the bin folder in your project folder. So it looks for an ini file with the same name as the exe file, in the "current directory".
Then set the value for section. I've added a commented example of how to add the key and value to a listbox.

The code will read line by line looking for the section. After it finds it it will read the following key/value lines until it reaches either an empty line or a new section - row value beginning with [
Okay I will try this code. and sorry for late reply...
My System SpecsSystem Spec
05 Feb 2015   #5
JBmorris

Windows Vista Business 32bit
 
 

Hmm, I am not sure if i did this right...
Code:
Dim inifile As String = System.AppDomain.CurrentDomain.BaseDirectory() & Replace(LCase(IO.Path.GetFileName(Application.ExecutablePath)), "WindowsApplication2.exe", "IDstorage.ini") <here is what i am confused on.
        Dim objReader As New System.IO.StreamReader(inifile, System.Text.Encoding.Default)
        Dim row As String
        Dim section As String = "list"
        Dim match As Boolean = False
        Do While objReader.Peek() <> -1
            row = objReader.ReadLine()
            If LCase(row) = "[" & LCase(section) & "]" Then
                match = True
            ElseIf match AndAlso Trim(row) <> "" AndAlso Mid(row, 1, 1) <> "[" Then
                Dim data() As String = Split(row, "=")
                Dim key = data(0)
                Dim value = data(1)
                Debug.Print(key & "=" & value)
                ListBox1.Items.Add(key & "=" & value)
            Else
                match = False
            End If
        Loop
        objReader.Close()
am i supposed to put in this
Quote:
My.Application.Info.DirectoryPath + "/inifle.ini"
for the ini value?
My System SpecsSystem Spec
06 Feb 2015   #6
Tookeri

Windows 7 Pro 32
 
 

This will be just fine:

Dim inifile As String = My.Application.Info.DirectoryPath + "/inifle.ini"

or even safer a hard coded value will work(as long as it's correct):

Dim inifile As String = "c:\foldername\filename.ini"
My System SpecsSystem Spec
06 Feb 2015   #7
JBmorris

Windows Vista Business 32bit
 
 

Quote   Quote: Originally Posted by Tookeri View Post
This will be just fine:

Dim inifile As String = My.Application.Info.DirectoryPath + "/inifle.ini"

or even safer a hard coded value will work(as long as it's correct):

Dim inifile As String = "c:\foldername\filename.ini"
It works!
Thanks Tookeri!
My System SpecsSystem Spec
Reply

 VB 2010 how to populate a listbox from an ini file




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
programs & features won't populate?
I'm running Win 7 64bit. I'm having my machine looked at this weekend (getting a lot of system errors). In the meantime I need to do some tuning that requires a little tweaking in "programs & features section. My problem is that when I go there (control panel> programs & features) the box pops up...
General Discussion
Control panel won't populate (gives blank screen)
I hope someone can help with this because I haven't found any answers. I have already tried system restore and it didn't help. When I open Control Panel, I can only use it to perform one function (e.g. change theme, etc.) When I click to open Control Panel again, it gives me a blank screen. ...
Customization
Quick Launch menu slow to populate
Hope this is in the right section. My quick launch menu takes up to 2 minutes to populater (even when there is no other software running). Once it has populated, then it will work again instantly. Any way to speed this up?
Performance & Maintenance
Ms Fax Cannot Populate Fields in cover pages (.cov)
Win 7 Pro 64. Ms Fax does not permit updating of fields in cover pages. Info like senders name, recipient name, etc. cannot be entered. The controls/fields can be selected, but, entering or updating info does not work. I managed to copy a cover page created under Ms Fax under Xp and CAN...
Software
Start Menu Icons slow to populate, help :(
I recently noticed that my desktop icons (8) and tray buttons (7) were re-populating a lot and werent that nippy either... so I tried the MaxCachedIcons hack AND then changed it to Max Cached Icons as I read the first one doesnt work... anyway nothing happened and I noticed that my Start Menu Icons...
Performance & Maintenance
Erase Auto-Populate history
Guys/Girls, Is there anyway to get the history of recently opened documents from showing up in the open documents?
Microsoft Office


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 10:02.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App