Results 1 to 3 of 3

Thread: Get value out of listbox string

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    1

    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.
    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

  2. #2
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    Re: Get value out of listbox string

    I may have overcomplicated this for you, but here's a quick stab at it.

    VB Code:
    1. Public Class Form1
    2.     Private albums As New ArrayList()
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.  
    6.         Dim m_album As album
    7.  
    8.         ListBox1.DisplayMember = "displayText"
    9.         ListBox1.ValueMember = "valuedecimal"
    10.  
    11.         m_album = New album("1 - Nickelback - Black Horse - 9.99$", 9.99)
    12.         albums.Add(m_album)
    13.         m_album = New album("2 - AC/DC - Black Ice - 12.99$", 12.99)
    14.         albums.Add(m_album)
    15.  
    16.         ListBox1.Sorted = True
    17.         ListBox1.DataSource = albums
    18.  
    19.     End Sub
    20.  
    21.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    22.         Dim total As Decimal
    23.  
    24.         For Each m_album As album In albums
    25.             total = total + m_album.valuedecimal
    26.  
    27.         Next
    28.  
    29.         MessageBox.Show(total.ToString & "$")
    30.  
    31.     End Sub
    32.  
    33.     Public Class album
    34.         Private m_displaytext
    35.         Private m_valuedecimal
    36.         Public ReadOnly Property displayText() As String
    37.             Get
    38.                 Return m_displaytext
    39.             End Get
    40.  
    41.         End Property
    42.         Public ReadOnly Property valuedecimal() As Decimal
    43.             Get
    44.                 Return m_valuedecimal
    45.             End Get
    46.         End Property
    47.  
    48.         Public Sub New(ByVal displayText As String, ByVal valuedecimal As Decimal)
    49.             m_displaytext = displayText
    50.             m_valuedecimal = valuedecimal
    51.         End Sub
    52.     End Class
    53. End Class
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width