First off I would recommend storing your data in a structured file such as XML as shown in figure 1 then read in the data using a DataSet object or if using VS2008 LINQ with XDocument as in figure 2.
A practical example of storing your data in a Listbox with all data displayed and the ID for each row stored with it which can be accessed via SelectedIndexChanged event of the listbox. This example to keep things simple loads static data. Your form needs a Listbox named Listbox1, a lable named Label1 (for the total of each row). Place the following code in your form and run it. Hope this helps
Code:Private Sub Goods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim SampleData = _ <?xml version="1.0"?> <Goods> <Item ID="1"> <Artist>Nickelback</Artist> <Album>Black Horse</Album> <Price>9.99</Price> </Item> <Item ID="2"> <Artist>AC/DC</Artist> <Album>Black Ice</Album> <Price>12.99</Price> </Item> </Goods> Dim DataSource = (From items In SampleData...<Item> Select New With _ { _ .Identifier = items.@ID, _ .Artist = items.<Artist>.Value, _ .Album = items.<Album>.Value, _ .Price = items.<Price>.Value, _ .Show = .Artist & " - " & .Album & " - " & .Price}).ToList ListBox1.DataSource = DataSource ListBox1.DisplayMember = "Show" ListBox1.ValueMember = "Identifier" Dim Amount = (From x In DataSource Select CType(x.Price, Integer)).Sum Label1.Text = Amount.ToString AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles ListBox1.SelectedIndexChanged Console.WriteLine("ID {0} for {1}", ListBox1.SelectedValue, ListBox1.Text) End Sub
Figure 1
Code:<?xml version="1.0"?> <Goods> <Item ID="1"> <Artist>Nickelback</Artist> <Album>Black Horse</Album> <Price>9.99</Price> </Item> <Item ID="2"> <Artist>AC/DC</Artist> <Album>Black Ice</Album> <Price>12.99</Price> </Item> </Goods>
Figure 2
Code:Dim GoodsFile As String = "Goods.xml" If IO.File.Exists(GoodsFile) Then Dim Document = XDocument.Load(GoodsFile) Dim DataSource = _ (From items In SampleData...<Item> Select New With _ { _ .Identifier = items.@ID, _ .Artist = items.<Artist>.Value, _ .Album = items.<Album>.Value, _ .Price = items.<Price>.Value, _ .Show = .Artist & " - " & .Album & " - " & .Price}).ToList Else MsgBox(String.Format(<text>Failed to locate '{0}'</text>.Value, GoodsFile)) End If




Reply With Quote