loading content of text file into a listbox

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

loading content of text file into a listbox

 
0
  #1
Oct 21st, 2008
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: loading content of text file into a listbox

 
0
  #2
Oct 22nd, 2008
Try this:
  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
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: loading content of text file into a listbox

 
0
  #3
Oct 22nd, 2008
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.
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: loading content of text file into a listbox

 
0
  #4
Oct 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: loading content of text file into a listbox

 
0
  #5
Oct 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 45
Reputation: cellus205 is an unknown quantity at this point 
Solved Threads: 1
cellus205 cellus205 is offline Offline
Light Poster

Re: loading content of text file into a listbox

 
0
  #6
Oct 22nd, 2008
You should be able to just split the string on the space character, and just loop through the array of items. Kinda like this:

  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: loading content of text file into a listbox

 
0
  #7
Oct 22nd, 2008
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

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: loading content of text file into a listbox

 
0
  #8
Oct 23rd, 2008
any ideas about this one guys?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 45
Reputation: cellus205 is an unknown quantity at this point 
Solved Threads: 1
cellus205 cellus205 is offline Offline
Light Poster

Re: loading content of text file into a listbox

 
0
  #9
Oct 24th, 2008
One way is you can store the prices in an array, and u can store those in the same loop as the items.

  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)
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: loading content of text file into a listbox

 
0
  #10
Oct 27th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC