Hi everyone. I'm new here and hope I can get a little advice on how to list my array into a ListBox. I have my structure and array of structures. I need help with a For Loop that will list the particular elements in the List Box. Any suggestions on how to best write this?
' I create a Structure with members
' place, title, author, publisher, price, description, lastweek, weeksonlist
Structure bestseller
Public place As Integer
Public title As String
Public author As String
Public publisher As String
Public price As Decimal
Public description As String
Public lastweek As Integer
Public weeksonlist As Integer
End Structure
' I then create an array of structures
Dim bookData() As bestseller
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btndisplay.Focus()
'I want to open the file books.dat and read each books information into the array.
'Of course, the first element of the array has the first book's information
'and I want to do this using a For loop
Dim FilePath As String = "books.dat"
Dim counter As Integer
Dim LoopInt As Integer
'check the file exists
If IO.File.Exists("books.dat") Then
'put all the lines into an array
Dim Lines() As String = IO.File.ReadAllLines("books.dat")
'resize the bookdata array to how many books in the file by dividing the lines by 8
Array.Resize(bookData, Lines.Length \ 8)
'loop the lines array but step every 8 places
For LoopInt = 0 To Lines.GetUpperBound(0) Step 8
'put all the lines in the right places
bookData(counter).place = Integer.Parse(Lines(LoopInt))
bookData(counter).title = Lines(LoopInt + 1)
bookData(counter).author = Lines(LoopInt + 2)
bookData(counter).publisher = Lines(LoopInt + 3)
bookData(counter).price = Decimal.Parse(Lines(LoopInt + 4))
bookData(counter).description = Lines(LoopInt + 5)
bookData(counter).lastweek = Integer.Parse(Lines(LoopInt + 6))
bookData(counter).weeksonlist = Integer.Parse(Lines(LoopInt + 7))
'increase the variable by one to keep the bookdata array stepping up one each time the
'loopint loops
counter = counter + 1
Next LoopInt
Else
MessageBox.Show("File does not exist", "Bestsellers", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub btndisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisplay.Click
' When clicking the Display button, I want to display in the ListBox
' each book's place, title, author, publisher and price using a for loop.
'This would be the heading.
listBooks.Items.Add("Place" & ControlChars.Tab & "Title" & ControlChars.Tab & ControlChars.Tab & ControlChars.Tab & "Author" & _
ControlChars.Tab & ControlChars.Tab & "Publisher" & ControlChars.Tab & ControlChars.Tab & ControlChars.Tab & "Price")
End Sub
Private Sub listbooks_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Now I will use listbox's selectedindex property to display the selected book's description,
'lastweek and weeksonlist in the respective labels
End Sub
Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
Me.Close()
End Sub
Private Sub lbldescription_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbldescription.Click
End Sub
End Class