how to get data from a checked ListViewBox vb.net

739 Views Asked by At

Hi :) basically i have a Checked ListViewBox (i selected "checked = true" in the properties section). I need to be able to get data from the checked items/rows in the listViewBox to calculate a total price in a text box. I have a combo box where you can select what file you want to be displayed in the ListViewBox (so the lvwbox can alternate between 3 different files but i can solve this using a simple If Statement - If cboDate = "....." Then.....etc. so it's not a problem)

I'll post a screenshot of my form on here so it makes more sense but basically, i need to code something that lets me select multiple items in the ListViewBox and then displays a total price based on those selections (btw each item in the Box has a different price)

The file names are JuneEvent, JulyEvent and AugEvent and each one has several record fields which are ClassID, ClassDescripion, ClassTime and ClassPrice.

I've been stuck on this for ages can someone please help me! thanks lol :P

p.s Currently the only code i have on the form is to display a certain file in the ListViewBox based what is selected in the combo box...

Click here to see the screenshot of program form

Here is the form in action - ignore the table on the right hand side - i'll deal with that later

EDIT: RECENT CODE:

Private Sub lvwEvents_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles lvwEvents.ItemChecked
        Dim totalAmt As Double = 0
        For Each lv As ListViewItem In lvwEvents.Items
            If lv.Checked = True Then
                totalAmt += JuneEvent.ClassPrice
            End If
        Next
        txtPrice.Text = FormatNumber(totalAmt, 2)
End Sub 
2

There are 2 best solutions below

4
Aethan On

Since you don't have a code posted for us to study, I'll just add here the code that will do what you need to do. I hope you can use it as your reference.

     Private Sub ListView1_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles ListView1.ItemChecked
        Dim totalAmt As Double = 0 
        For Each lv As ListViewItem In ListView1.Items
            If lv.Checked = True Then
                totalAmt += Val(lv.Text)
            End If
        Next
        lblLabelForYourTotalAmt.Text = FormatNumber(totalAmt,2)
     End Sub

I assume here that ListView1.Items(0).Text is the amount to be summed.

0
Alex Bradley On

My friend managed to fix it for me: this is the final result:

  Private Sub lvwEvents_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles lvwEvents.ItemChecked
    Dim totalAmt As Double = 0
    'check what items are checked in list view
    For Each lv As ListViewItem In lvwEvents.Items
        If lv.Checked = True Then
            'add checked items value to total
            totalAmt += Double.Parse(lv.SubItems(4).Text.Substring(1))
        End If
    Next
    'display total price
    txtPrice.Text = FormatNumber(totalAmt, 2)
End Sub