Welcome to Dream.In.Code
Become a VB Expert!

Join 137,262 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,502 people online right now. Registration is fast and FREE... Join Now!




reading file into an array, then adding array to listbox

 
Reply to this topicStart new topic

reading file into an array, then adding array to listbox

lab3tech
28 Nov, 2006 - 10:14 AM
Post #1

New D.I.C Head
*

Joined: 28 Oct, 2006
Posts: 46


My Contributions
I have now been able to go back to working on my VB class, and have a question for the forum. I have a file which contains names of cars and rental prices. I have the file set up as follows:
Ford Taurus|
85|
MonteCarlo|
65|
etc. I put the bar after the names and rental prices as I want then to display on different lines in the array. I also have added code to check to make sur the file can be opened, and for a message box to display if there are problems. As I want this information to be displayed to the user when the form opens, I have the code in form load. I also have built a custom control which includes two datetimepickers and labels; the names of the labels are entered when the form loads. My file is found, as I do not get any warnings as I do when I enter a non-existant file. The problem comes when I add the array to the listbox - what prints is "System String()". My code is as follows:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sFileToCheck As String = (Application.StartupPath & "\vehicles.doc")
Dim word As String
Dim items As String()
Dim i As Integer
Dim upperBound As Integer

If Not File.Exists(Application.StartupPath & "\vehicles.doc") Then
MessageBox.Show("Cannot find the file. Please try again!", "File Error Warning", _
MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If

Dim sr As IO.StreamReader = IO.File.OpenText(Application.StartupPath & "\vehicles.doc")
upperBound = 0
Do While (sr.Peek <> -1)
word = sr.ReadLine
upperBound += 1
Loop
sr.Close()

ReDim items(upperBound)
sr = IO.File.OpenText(Application.StartupPath & "\vehicles.doc")
For i = 1 To upperBound
items(i) = sr.ReadLine
Next
sr.Close()

LstRent.Items.Add(items)

DateRange1.begindate = "Pick-up Date:"
DateRange1.enddate = "Return Date:"
End Sub

I am making an array from the file, and then loading the array into the listbox so I can find the index of the car that is selected from the lsitbox, and then find the rental price (car index + 1) to use in calculating rental fees. I will continue to work on this, but amy assistance would be greatly appreciated.

User is offlineProfile CardPM
+Quote Post

m2s87
RE: Reading File Into An Array, Then Adding Array To Listbox
28 Nov, 2006 - 03:58 PM
Post #2

D.I.C Regular
Group Icon

Joined: 28 Nov, 2006
Posts: 390



Thanked: 1 times
Dream Kudos: 1225
My Contributions
QUOTE(lab3tech @ 28 Nov, 2006 - 11:14 AM) *

I have the file set up as follows:
Ford Taurus|
85|
MonteCarlo|
65|
etc.


Are you familiar with the term XML? (if not, learn it in w3schools)
Your file should look like
<cars>
<car><name>Ford Taurus</name><price>85</price></car>
<car><name>MonteCarlo</name><price>65</price></car>
</cars>

You can actually use that idea, where selected element in listbox, retrives the index. It can be used in selecting the correct child node and useing it in your calculations.

This post has been edited by m2s87: 28 Nov, 2006 - 04:00 PM
User is offlineProfile CardPM
+Quote Post

lab3tech
RE: Reading File Into An Array, Then Adding Array To Listbox
28 Nov, 2006 - 11:31 PM
Post #3

New D.I.C Head
*

Joined: 28 Oct, 2006
Posts: 46


My Contributions
QUOTE(m2s87 @ 28 Nov, 2006 - 04:58 PM) *

QUOTE(lab3tech @ 28 Nov, 2006 - 11:14 AM) *

I have the file set up as follows:
Ford Taurus|
85|
MonteCarlo|
65|
etc.


Are you familiar with the term XML? (if not, learn it in w3schools)
Your file should look like
<cars>
<car><name>Ford Taurus</name><price>85</price></car>
<car><name>MonteCarlo</name><price>65</price></car>
</cars>

You can actually use that idea, where selected element in listbox, retrives the index. It can be used in selecting the correct child node and useing it in your calculations.


Thank you! I will try that this afternoon and let you know how it works!!

User is offlineProfile CardPM
+Quote Post

lab3tech
RE: Reading File Into An Array, Then Adding Array To Listbox
29 Nov, 2006 - 12:17 PM
Post #4

New D.I.C Head
*

Joined: 28 Oct, 2006
Posts: 46


My Contributions
QUOTE(lab3tech @ 29 Nov, 2006 - 12:31 AM) *

QUOTE(m2s87 @ 28 Nov, 2006 - 04:58 PM) *

QUOTE(lab3tech @ 28 Nov, 2006 - 11:14 AM) *

I have the file set up as follows:
Ford Taurus|
85|
MonteCarlo|
65|
etc.


Are you familiar with the term XML? (if not, learn it in w3schools)
Your file should look like
<cars>
<car><name>Ford Taurus</name><price>85</price></car>
<car><name>MonteCarlo</name><price>65</price></car>
</cars>

You can actually use that idea, where selected element in listbox, retrives the index. It can be used in selecting the correct child node and useing it in your calculations.


Thank you! I will try that this afternoon and let you know how it works!!

I just checked my textbook, and we did not cover the section on XML in the class, so I cannot use XML for my program. Any other suggestions?

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 07:03PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month