loading content of text file into a listbox
I need to load the contents of a text file (itemInfo.txt) into a list box, I am on the wrong track, can someone please help.
Public Class MainForm
Private path As String = "L:\Visual Basic\SequentialHW\"
Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
itemListBox.Items.Clear()
itemListBox.Items.Add(path & "itemInfo.txt")
End Sub
bpacheco1227
Junior Poster in Training
58 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
It worked thanks, your code worked and I had the wrong path.
This is the information that is in the itemInfo.txt file
Shirt 11.99
Shoes 14.99
Jacket 25.99
Socks 8.99
Gloves 4.99
I actually should have specified that I only need to load the item name into the list box and upon clicking the item name the price shows up in a separate label box. Any help would be greatly appreciated again, thank you.
bpacheco1227
Junior Poster in Training
58 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
anyone have any thoughts on this one I can't figure out how to have the program recognize just the first word and reference the price that's associated with that item and place just that price in a label box.
bpacheco1227
Junior Poster in Training
58 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
You should be able to just split the string on the space character, and just loop through the array of items. Kinda like this:
Dim paths() As String
Dim path As String = "www.test.com"
Dim itemlistbox As ListBox
Dim num As Integer
paths = Split(My.Computer.FileSystem.ReadAllText(path & "itemInfo.txt"), vbNewLine)
For J = 0 To paths.Length()
num = paths(J).IndexOf(Chr(32))
itemlistbox.Items.Add(paths(J).Substring(0, num + 1))
Next
cellus205
Junior Poster in Training
57 posts since May 2007
Reputation Points: 10
Solved Threads: 3
thanks for the help but I need Daniweb just a little more , ok I got to segregate the line into itemname now how do I display the price in a label called priceLabel upon clicking the appropriate item name
Public Class MainForm
'Private path As String = "L:\Visual Basic\"
Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim paths() As String
Dim path As String = "L:\Visual Basic\"
Dim i As Integer
' Dim itemlistbox As ListBox
Dim num As Integer
'Dim a As String = My.Computer.FileSystem.ReadAllText(path & "itemInfo.txt")
'Dim b As String() = a.Split(CChar(vbNewLine))
paths = Split(My.Computer.FileSystem.ReadAllText(path & "itemInfo.txt"), vbNewLine)
'itemListBox.Items.Clear()
'itemListBox.Items.AddRange(b)
For i = 0 To paths.Length()
num = paths(i).IndexOf(Chr(32))
itemlistbox.Items.Add(paths(i).Substring(0, num + 1))
Next
'itemListBox.Items.Clear()
'My.Computer.FileSystem.ReadAllText(path & "itemInfo.txt")
'itemListBox.Items.Add(path & "itemInfo.txt")
End Sub
sorry for all the comments I was commenting things in and out alot
bpacheco1227
Junior Poster in Training
58 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
any ideas about this one guys?
bpacheco1227
Junior Poster in Training
58 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
One way is you can store the prices in an array, and u can store those in the same loop as the items.
Dim prices() As String
For i = 0 To paths.Length()
num = paths(i).IndexOf(Chr(32))
itemlistbox.Items.Add(paths(i).Substring(0, num + 1))
prices(i) = paths(i).Substring(num + 1)
Next
Then in the function that handles the itemListbox.Selected event, just add
MyLabel.Text = prices(itemListBox.SelectedIndex)
cellus205
Junior Poster in Training
57 posts since May 2007
Reputation Points: 10
Solved Threads: 3
Thanks for the reply but when I try the code like this I get a "Warning 1 Variable 'prices' is used before it has been assigned a value. A null reference exception could result at runtime. L:\Visual Basic\SequentialHW\Form1.vb 24 13 SequentialHW"
I don't understand why prices would not have a value at this point in the code. Any further assistance would be greatly appreciated.
bpacheco1227
Junior Poster in Training
58 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
What does it do when you try to run it? I think thats just a warning, but it would actually have the value when you run it.
cellus205
Junior Poster in Training
57 posts since May 2007
Reputation Points: 10
Solved Threads: 3
it just gives me one of the items (the first one I think) and when I click on it it doesn't put anything in the price label. I also just noticed that I get this when I run it "A first chance exception of type 'System.NullReferenceException' occurred in SequentialHW.exe"
bpacheco1227
Junior Poster in Training
58 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0