read file / combobox

f15e

Member
Joined
May 4, 2006
Messages
21
Programming Experience
Beginner
Is it possible to read a txt file which has a list of items in it and read it into a combobox? I can read a file in but can't get the list to show up in the combobox. If it can be done, how? Thanks.
 
the easiest way is when you read the file lines add them to the combo box:

ComboBox1.Items.Add(FileLine)
 
Here is the code I have so far:
VB.NET:
Imports System.IO

Public Class Form1

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        Dim objStreamReader As StreamReader
        Dim strLine As String

        'Pass the file path and the file name to the StreamReader constructor.
        objStreamReader = New StreamReader("C:\test.txt")

        'Read the first line of text.
        strLine = objStreamReader.ReadLine

        'Continue to read until you reach the end of the file.
        Do While Not strLine Is Nothing

            'Write the line to the ComboBox.
            ComboBox1.Items.Add(strLine)

            'Read the next line.
            strLine = objStreamReader.ReadLine
        Loop

        'Close the file.
        objStreamReader.Close()

    End Sub
End Class
I can't get the text from within my test.txt file to show up in the combobox. What am I doing wrong? Thanks for the help!
 
Last edited by a moderator:
i see the problem, you've got the code in the combobox's selectedindexchanged event, which means if the index never changes that code will never run, if the combo box is empty (which it is) then the index will never change

you need to put the code elsewhere, like a button or menu's click event, form's load event etc.. i personally would put it in the form's load event:
VB.NET:
Imports System.IO

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim objStreamReader As New StreamReader("C:\test.txt")
        Do While objStreamReader.Peek <> -1
            'Read the line to the ComboBox.
            ComboBox1.Items.Add(objStreamReader.ReadLine())
        Loop
        objStreamReader.Close()
    End Sub
End Class
 
Now that the combobox list has been loaded from the .txt file, I need some hints on taking the items in the combobox and being able to open files with whatever item I choose in the list. When the items have been read into the combobox, I now need to associate it with an address to a certain file on my computer. For example, let's say I choose the "VB.NET Tutorial" item in the list of items within the combobox. It would then open that file using the Process.Start(someFile address).

Therefore once items are read in from the .txt file, I need to associate them with some address. I was thinking of putting the file addresses in a separate .txt file and reading them in, but then I need to associate the correct address with its correct item within the combobox. I'm not sure how to do that. By doing this, all I would need to do is add items and addresses to the .txt files and not have to alter the code. Thanks.
 
if the first file has not only the items, but also the addresses to the other files then you can read both values in, i would suggest a csv type file (comma delimited) with the format of the contents:
Item,Address

and when you read in each line, just separate it by the comma and add one value to the combobox and store the addresses in an ArrayList then when the user picks a value in the combobox you can use the SelectedIndex to get the address from the ArrayList

i could make a quick example if needed, i hope i explained it clear enough
 
Thanks Juggalo! I will try that suggestion. I appreciate your help. I haven't done any VB in quite a while and trying to get back into it.
 
Juggalo, I have added some code but not sure how to use the SelectedIndex. I looked online, but didn't seem to find anything that helped. Not sure if the rest of my code is correct. It is incomplete though. How is the selectedindex used here.
VB.NET:
Imports System.IO
Imports System.NullReferenceException

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim objStreamReader As New StreamReader("C:\test.txt")
        Do While objStreamReader.Peek <> -1
            'Read the line to the ComboBox.
            ComboBox1.Items.Add(objStreamReader.ReadLine())
        Loop
        objStreamReader.Close()



        Dim objStreamReader2 As New StreamReader("C:\address.txt")
        Dim ADDRESS As New ArrayList()
        Dim x As Integer



        Do While objStreamReader2.Peek <> -1

            'Read Addresses into the ArrayList.
            ADDRESS.Add(objStreamReader2.ReadLine())

        Loop

        objStreamReader2.Close()

        'need to put the selectedindex here?

        Process.Start(something here)


    End Sub
End Class

thanks!!!
 
Last edited by a moderator:
i wrote a quick example and the zip file contains only the form and the test.txt file (i made one real quick) but this is more of what i'm talking about

also when posting code, please use the [ code] and [ /code] tags, it makes things easier to read
 
Back
Top