Get value out of listbox string
Hello.
I've searched the web for a while, without finding what I'm looking for. I hope somebody here can help me. I'm a beginner, so my explanation might be a bit dumb, but i hope you know what I mean.
I have a listbox listing goods (CD's) with information
Idnumber, artist, album, price etc.
so that it's listed like this
1 - Nickelback - Black Horse - 9.99$
2 - AC/DC - Black Ice - 12.99$
etc.
Now. I'm wondering if there is any way to get for example the price out of every string, and adding these together, posting a sum at the bottom of the listbox.
So that the listbox will show
1 - Nickelback - Black Horse - 9.99$
2 - AC/DC - Black Ice - 12.99$
SUM - 22.98$
My problem is to get the specific data out of the string.:confused:
I do not have any code-example to poste, because I couldn't find anything that seemed to lead in this direction. English is not my mother language, so I might have searched with wrong keywords.
Anyway. I hope somebody can give me some answers :)
Grominj
Re: Get value out of listbox string
I may have overcomplicated this for you, but here's a quick stab at it.
VB Code:
Public Class Form1
Private albums As New ArrayList()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim m_album As album
ListBox1.DisplayMember = "displayText"
ListBox1.ValueMember = "valuedecimal"
m_album = New album("1 - Nickelback - Black Horse - 9.99$", 9.99)
albums.Add(m_album)
m_album = New album("2 - AC/DC - Black Ice - 12.99$", 12.99)
albums.Add(m_album)
ListBox1.Sorted = True
ListBox1.DataSource = albums
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim total As Decimal
For Each m_album As album In albums
total = total + m_album.valuedecimal
Next
MessageBox.Show(total.ToString & "$")
End Sub
Public Class album
Private m_displaytext
Private m_valuedecimal
Public ReadOnly Property displayText() As String
Get
Return m_displaytext
End Get
End Property
Public ReadOnly Property valuedecimal() As Decimal
Get
Return m_valuedecimal
End Get
End Property
Public Sub New(ByVal displayText As String, ByVal valuedecimal As Decimal)
m_displaytext = displayText
m_valuedecimal = valuedecimal
End Sub
End Class
End Class
Re: Get value out of listbox string
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