943,712 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 7612
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 21st, 2008
0

loading content of text file into a listbox

Expand Post »
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.

VB Syntax (Toggle Plain Text)
  1. Public Class MainForm
  2. Private path As String = "L:\Visual Basic\SequentialHW\"
  3.  
  4. Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.  
  6. itemListBox.Items.Clear()
  7. itemListBox.Items.Add(path & "itemInfo.txt")
  8.  
  9. End Sub
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bpacheco1227 is offline Offline
58 posts
since Jul 2007
Oct 22nd, 2008
0

Re: loading content of text file into a listbox

Try this:
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Dim a As String = My.Computer.FileSystem.ReadAllText(path & "itemInfo.txt")
  3. Dim b As String() = a.Split(vbNewLine)
  4. itemListBox.Items.AddRange(b)
  5. End Sub
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002
Oct 22nd, 2008
0

Re: loading content of text file into a listbox

or if you want to do it in one line:
itemListBox.Items.AddRange(Split(My.Computer.FileSystem.ReadAllText(path & "itemInfo.txt"), vbNewLine))
Last edited by waynespangler; Oct 22nd, 2008 at 7:22 am.
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002
Oct 22nd, 2008
0

Re: loading content of text file into a listbox

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.
Last edited by bpacheco1227; Oct 22nd, 2008 at 9:55 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bpacheco1227 is offline Offline
58 posts
since Jul 2007
Oct 22nd, 2008
0

Re: loading content of text file into a listbox

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bpacheco1227 is offline Offline
58 posts
since Jul 2007
Oct 22nd, 2008
0

Re: loading content of text file into a listbox

You should be able to just split the string on the space character, and just loop through the array of items. Kinda like this:

VB Syntax (Toggle Plain Text)
  1.  
  2. Dim paths() As String
  3. Dim path As String = "www.test.com"
  4. Dim itemlistbox As ListBox
  5. Dim num As Integer
  6.  
  7. paths = Split(My.Computer.FileSystem.ReadAllText(path & "itemInfo.txt"), vbNewLine)
  8.  
  9. For J = 0 To paths.Length()
  10. num = paths(J).IndexOf(Chr(32))
  11. itemlistbox.Items.Add(paths(J).Substring(0, num + 1))
  12. Next
Last edited by cellus205; Oct 22nd, 2008 at 5:25 pm. Reason: code tags
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007
Oct 22nd, 2008
0

Re: loading content of text file into a listbox

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

VB Syntax (Toggle Plain Text)
  1. Public Class MainForm
  2. 'Private path As String = "L:\Visual Basic\"
  3.  
  4. Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.  
  6. Dim paths() As String
  7. Dim path As String = "L:\Visual Basic\"
  8. Dim i As Integer
  9. ' Dim itemlistbox As ListBox
  10. Dim num As Integer
  11. 'Dim a As String = My.Computer.FileSystem.ReadAllText(path & "itemInfo.txt")
  12. 'Dim b As String() = a.Split(CChar(vbNewLine))
  13.  
  14. paths = Split(My.Computer.FileSystem.ReadAllText(path & "itemInfo.txt"), vbNewLine)
  15. 'itemListBox.Items.Clear()
  16. 'itemListBox.Items.AddRange(b)
  17. For i = 0 To paths.Length()
  18. num = paths(i).IndexOf(Chr(32))
  19. itemlistbox.Items.Add(paths(i).Substring(0, num + 1))
  20. Next
  21.  
  22. 'itemListBox.Items.Clear()
  23. 'My.Computer.FileSystem.ReadAllText(path & "itemInfo.txt")
  24.  
  25. 'itemListBox.Items.Add(path & "itemInfo.txt")
  26.  
  27. End Sub
sorry for all the comments I was commenting things in and out alot
Last edited by bpacheco1227; Oct 22nd, 2008 at 11:47 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bpacheco1227 is offline Offline
58 posts
since Jul 2007
Oct 23rd, 2008
0

Re: loading content of text file into a listbox

any ideas about this one guys?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bpacheco1227 is offline Offline
58 posts
since Jul 2007
Oct 24th, 2008
0

Re: loading content of text file into a listbox

One way is you can store the prices in an array, and u can store those in the same loop as the items.

VB Syntax (Toggle Plain Text)
  1. Dim prices() As String
  2.  
  3. For i = 0 To paths.Length()
  4.  
  5. num = paths(i).IndexOf(Chr(32))
  6.  
  7. itemlistbox.Items.Add(paths(i).Substring(0, num + 1))
  8. prices(i) = paths(i).Substring(num + 1)
  9.  
  10. Next

Then in the function that handles the itemListbox.Selected event, just add

MyLabel.Text = prices(itemListBox.SelectedIndex)
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007
Oct 27th, 2008
0

Re: loading content of text file into a listbox

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.
Last edited by bpacheco1227; Oct 27th, 2008 at 2:09 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bpacheco1227 is offline Offline
58 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Sample Project Uploaded
Next Thread in VB.NET Forum Timeline: get the selected row in datagridview





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC