473,320 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Need Help Listing an array into a ListBox in VB.Net

4
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
Nov 30 '06 #1
0 5556

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: MFC | last post by:
Ok, after three C# books, (C# How to Program, Programming in the Key of C#, and C# Weekend Crash Course) and three weeks, I believe I have tried everything to make a certain form function...
11
by: Zorpiedoman | last post by:
The problem is this: I have a list box. I set an array list as the datasource. I remove an item from the array list. I set the listbox datasource to nothing. I set the listbox datasource to...
6
by: Paul | last post by:
Hi All, Framework 1.1 listbox control unable to DataBind I've been googling for an answer to this query that appears quite a lot, but none, it seem, answers my problem directly. I am...
2
by: vbsourcer | last post by:
Hello all could anybody tell me how I would be able to list information from a database using Two Fields from the Database ? I have this code which will populate the listbox with exactly what I want...
7
by: vbsourcer | last post by:
Listing two fields -------------------------------------------------------------------------------- Hello all could anybody tell me how I would be able to list information from a database using...
0
by: vbsourcer | last post by:
Hello all could anybody tell me how I would be able to list information from a database using Two Fields from the Database ? I have this code which will populate the listbox with exactly what I want...
2
by: raypjr | last post by:
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...
45
by: angelicdevil | last post by:
i have 2 tables 1 is status_type with field name status and other is users with field username and status now i want that the first listbox lists all status from status type ( this i have achieved...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.