MultiSelect List Boxes

I have created a form with three multi-select list boxes and hidden  boxes to house the selections. I would like help with code that would allow me to sum each hidden box then multiply the sums by each other to get an answer when the "calculate" button is clicked

Answer this question

MultiSelect List Boxes

  • Harold1000

    I have three list boxes with percentages in them and would like to add the percentages in each box to get a total then mutliply the results to get another sum.
  • eugrus

    Here's an example with 2 listboxes with objects loaded into each of them.   When you click the button it will calculate the total of all the selected values in both listboxes.

    If you have databound items then you could total up the .SelectedValue properties.

    Hope this helps.

     

    Public Class Form1
        Private details As New ArrayList


        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            '//Add Items to the Drop Down as ClsPair Items
            Dim Obj As clsPricePair

            Obj = New clsPricePair("Fred", 1)
            ListBox1.Items.Add(Obj)

            Obj = New clsPricePair("Bill", 2)
            ListBox1.Items.Add(Obj)

            Obj = New clsPricePair("John", 3)
    Public Class Form1
        Private details As New ArrayList


        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '//Add Items to the Drop Down as ClsPair Items
            Dim Obj As clsPricePair

            Obj = New clsPricePair("Fred", 1)
            ListBox1.Items.Add(Obj)
            Obj = New clsPricePair("Bill", 2)
            ListBox1.Items.Add(Obj)
            Obj = New clsPricePair("John", 3)
            ListBox1.Items.Add(Obj)


            Obj = New clsPricePair("Mary", 1)
            ListBox2.Items.Add(Obj)
            Obj = New clsPricePair("Anne", 2)
            ListBox2.Items.Add(Obj)
            Obj = New clsPricePair("Karen", 3)
            ListBox2.Items.Add(Obj)
        End Sub


        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim iTotalCount As Integer = 0

            iTotalCount = CalculateTotal(ListBox1)
            iTotalCount = iTotalCount + CalculateTotal(ListBox2)

            Label1.Text = CType(iTotalCount, String)

        End Sub

        Private Function CalculateTotal(ByVal Control As ListBox) As Integer
            '//Display all the items which were moved
            Dim iCount As Integer = 0
            For Each ObjPair As clsPricePair In Control.SelectedItems
                iCount = iCount + ObjPair.price
            Next
            Return Icount
        End Function
    End Class

     

     

    '//Price Pair Item
    Public Class clsPricePair
        Private strName As String
        Private DecPrice As Decimal
        Public Sub New(ByVal name As String, ByVal price As Decimal)
            Me.name = name
            Me.price = price
        End Sub

        Public Property name() As String
            Get
                Return strName
            End Get
            Set(ByVal value As String)
                strName = value
            End Set
        End Property

        Public Property price() As Decimal
            Get
                Return DecPrice
            End Get
            Set(ByVal value As Decimal)
                DecPrice = value
            End Set
        End Property
        Public Overrides Function tostring() As String
            Return strName
        End Function
    End Class


  • djshades2004

    Sounds like you want someone to tell you how to do the math. Without a reference to actual application to what your doing i had to find one of my own.

    And I have. Such as Markup percentages. Depending on how you can do it legaly, if real money is inovled, I suggest you check the correct method. Meanwhile I'm going to go with virtual here. We can do whatever then and perhaps this is the results you are looking for.

    Adding percentages together for a single result percentage to use againts an actual value.

    from your list. Assuming your using text such as "12%", in the data column you could store the math value equivelent such a .12 or 0.12 which ever makes more sense to you, to use in your calculations.

    Say three have been selected. You do something like:
     
    Dim iSum,iValue as integer
    For Each iValue in List.Selections(Data colum) 'Please note this is not actual code.
       isum=isum+ivalue
    next

    then you use the iSum against a number or simply display it, or use it to update something else accordingly.

    With more info, or some code pasted, I might be able to help more.

    Randy

  • Johan Traa

    Could you please explain a little more... What is in the listboxes What do you want to calculate

  • MultiSelect List Boxes