Fill A Combox From A Text File

Domino2006

Member
Joined
Jan 31, 2007
Messages
5
Programming Experience
1-3
Hi Guys and Gals

Im Trying to load the contents of a text file into a combobox, i have looked all over the net to no avail.

i have read the text file using a streamreader and assigned it to a string, i have used the string name as the datasource as follows

combobox1.datasource = stringname

it isnt a 1-dimensional array could this be the problem?


any help would be gr8 thanx in advance.

Domino2006
 
A combobox has a list of ITEMS. How is your text file broken down into items? If you can paste some of the content or something similar with bogus data, we may be able to help further.
 
Hi Me Again

The text file isnt broken down into any particular format, just each item is on a seperate line.

The Code below was posted on another site by a kind fellow coder, do you thing this might work?



Dim myArrList as New ArrayList
{..}
'the code to read content of the text file (line by line means not using readToEnd method)
myArrList.Add(line)
{...}



then


Dim i As Integer
For i = 0 To myArrList.Count - 1
Me.ComboBox1.Items.Add(myArrList(i))
Next
 
Why would you use the intermediary arraylist, does that serve a purpose?
You could use one of these one-liners:
VB.NET:
ComboBox1.DataSource = My.Computer.FileSystem.ReadAllText("items.txt").Split(vbNewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
'or
ComboBox1.Items.AddRange(My.Computer.FileSystem.ReadAllText("items.txt").Split(vbNewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries))
 
Domino...:

Yes, I think the code you posted would work if you created an array and read your file into the array.

And, here is another way.

Just add this code to a button click event or form load event or where ever you want it to run from. Then add the path to your file.

If your file has a txt file extension, it should work.
VB.NET:
Dim sr AsNew IO.StreamReader("C:\TestFiles\Testfile1.txt")
Dim lines AsString() = System.Text.RegularExpressions.Regex.Split(sr.ReadToEnd(), Environment.NewLine)
Dim parts AsString()
ForEach line AsStringIn lines
ComboBox1.Items.Add(line) '(0) & " " & lines(0))
Next line
sr.Close()

Good Luck
 
PS if youre doingthis for configuration reasons, then you can always bind the .Items property to a StringCollection entry in your app settings.... Its even easier than the methods already mentioned
 
Hello
I did the following to fill a combo box from a text file with world countries
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Combobox1.Items.AddRange (File.ReadAllLines("E:\VB Programming\Countries.txt"))
End Sub
 

Latest posts

Back
Top