| | |
loading content of text file into a listbox
Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2007
Posts: 58
Reputation:
Solved Threads: 0
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)
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
•
•
Join Date: Dec 2002
Posts: 461
Reputation:
Solved Threads: 56
Try this:
VB.NET Syntax (Toggle Plain Text)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a As String = My.Computer.FileSystem.ReadAllText(path & "itemInfo.txt") Dim b As String() = a.Split(vbNewLine) itemListBox.Items.AddRange(b) End Sub
Wayne
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
•
•
Join Date: Dec 2002
Posts: 461
Reputation:
Solved Threads: 56
or if you want to do it in one line:
itemListBox.Items.AddRange(Split(My.Computer.FileSystem.ReadAllText(path & "itemInfo.txt"), vbNewLine))
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.
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
•
•
Join Date: Jul 2007
Posts: 58
Reputation:
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.
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.
•
•
Join Date: May 2007
Posts: 45
Reputation:
Solved Threads: 1
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)
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
Last edited by cellus205; Oct 22nd, 2008 at 5:25 pm. Reason: code tags
•
•
Join Date: Jul 2007
Posts: 58
Reputation:
Solved Threads: 0
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
sorry for all the comments I was commenting things in and out alot
VB Syntax (Toggle Plain Text)
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
Last edited by bpacheco1227; Oct 22nd, 2008 at 11:47 pm.
•
•
Join Date: May 2007
Posts: 45
Reputation:
Solved Threads: 1
One way is you can store the prices in an array, and u can store those in the same loop as the items.
Then in the function that handles the itemListbox.Selected event, just add
MyLabel.Text = prices(itemListBox.SelectedIndex)
VB Syntax (Toggle Plain Text)
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)
•
•
Join Date: Jul 2007
Posts: 58
Reputation:
Solved Threads: 0
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.
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.
![]() |
Other Threads in the VB.NET Forum
- Previous Thread: update record from vb.net2003 to Ms access 2003
- Next Thread: Add New Item
Views: 3430 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
"crystal .net .net2005 30minutes 2008 access application arithmetic array assignment basic binary box button buttons center code combobox component connectionstring convert cpu data database databasesearch datagrid datagridview design designer dissertation dissertations dissertationthesis dll dosconsolevb.net editvb.net error excel firewall folder highlighting image images isnumericfuntioncall listview login math memory mobile ms mssqlbackend mysql navigate net opacity pan peertopeervideostreaming picturebox picturebox1 plugin port print printing printpreview problem problemwithinstallation project record reports" reuse save savedialog serial server sorting sql storedprocedure string structures studio temp textbox timer updown upload useraccounts usercontrol vb vb.net vb.netcode vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web wpf





