| | |
loading content of text file into a listbox
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
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
| Thread Tools | Search this Thread |
"crystal .net 2008 access add advanced application array assignment basic beginner box browser button buttons center click client code combo convert cpu cuesent data database datagrid datagridview datetimepicker designer dissertation dissertations dissertationtopic dosconsolevb.net eclipse editvb.net employees excel exists firewall forms html images isnumericfuntioncall listview map math memory mobile module msaccess mssqlbackend mysql navigate net number opacity open pan pdf picturebox picturebox2 port print printpreview record regex reuse right-to-left save search serial settings socket sorting sqldatbase sqlserver storedprocedure temp textbox timer transparency txttoxmlconverter upload useraccounts usercontol vb vb.net vb.nettoolboxvisualbasic2008sidebar vba vbnet vista visual visualbasic visualbasic.net visualstudio.net web winsock wpf wrapingcode xml





