none
Sum up or using functions of the values in ListBox

    Question

  • I am facing a problem which I don't know how to sum up or use a function on all the values listed in ListBox.

    I am still new in Visual Basic so I need some help and I hope it is as simple as possible.

    This is a function of counting equivalent capacitance value of a combined circuit, the values are listed in listbox and I want to use a function.

    This is the code for my function part, I know it sounds silly but I have no any idea of coding it.

     'Limited to 5 values of capacitance
        sum1 = Val(ListBox2.SelectedIndex = 0)
        sum2 = Val(ListBox2.SelectedIndex = 1)
        sum3 = Val(ListBox2.SelectedIndex = 2)
        sum4 = Val(ListBox2.SelectedIndex = 3)
        sum5 = Val(ListBox2.SelectedIndex = 4)
    
        If ComboBox1.SelectedIndex = 0 Then
            'Calculating Equivalent Capacitance in Series
            eq = (1 / sum1) + (1 / sum2) + (1 / sum3) + (1 / sum4) + (1 / sum5)
    
            CTB2.Text = Format(eq, "0.00")
          ElseIf ComboBox1.SelectedIndex = 1 Then
            'Calculating Equivalent Capacitance in Parallel
            eq = sum1 + sum2 + sum3 + sum4 + sum5
            CTB2.Text = Format(eq, "0.00")
          End If
    
    Thanks.

    Tuesday, December 21, 2010 6:40 AM

Answers

  • Just loop through the list box to get the items (selected or all).

    For looping through all items:

    For index As Integer = 0 To ListBox1.Items.Count - 1
            sum +=   (1 / ListBox1.Items(index))
    Next

     

    You can limit it to first 5 items also:

    For index As Integer = 0 To 5
            sum +=   (1 / ListBox1.Items(index))
    Next

    Is this what you are looking for?

    Happy to Help :)
    • Proposed as answer by Cor LigthertMVP Tuesday, December 21, 2010 9:46 AM
    • Marked as answer by SouLReaPeR1992 Tuesday, December 21, 2010 12:37 PM
    Tuesday, December 21, 2010 8:11 AM
  • OK, that helps. I couldn't tell from your code what you were trying to do, especially the use of ListBox2.SelectedIndex

    try this - it will work no matter how many values are in the ListBox

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
        Dim eq As Double = 0
    
        If ComboBox1.SelectedIndex = 0 Then
          'Calculating Equivalent Capacitance in Series
    
          For Each d As Double In ListBox2.Items
            eq += 1 / d
          Next
    
        Else
          'Calculating Equivalent Capacitance in Parallel
    
          For Each d As Double In ListBox2.Items
            eq += d
          Next
    
        End If
    
        CTB2.Text = Format(eq, "0.00")
        
      End Sub
    
    • Marked as answer by SouLReaPeR1992 Tuesday, December 21, 2010 12:37 PM
    Tuesday, December 21, 2010 11:15 AM

All replies

  • what are all the values in ListBox2?

    and is the user able to select 5 items from that?

    Please clarify a little more the flow of the app

    Tuesday, December 21, 2010 6:54 AM
  • Just loop through the list box to get the items (selected or all).

    For looping through all items:

    For index As Integer = 0 To ListBox1.Items.Count - 1
            sum +=   (1 / ListBox1.Items(index))
    Next

     

    You can limit it to first 5 items also:

    For index As Integer = 0 To 5
            sum +=   (1 / ListBox1.Items(index))
    Next

    Is this what you are looking for?

    Happy to Help :)
    • Proposed as answer by Cor LigthertMVP Tuesday, December 21, 2010 9:46 AM
    • Marked as answer by SouLReaPeR1992 Tuesday, December 21, 2010 12:37 PM
    Tuesday, December 21, 2010 8:11 AM
  •    

    It is a listbox that shows several values after I counted something else from other form and transferred to this listbox.

    I hope I can do more than 5 values. The code above is something I have done when I have no idea of this so I limit until 5 to make myself easier.

    Tuesday, December 21, 2010 9:15 AM
  • sum +=   (1 / ListBox1.Items(index))

     

    is this the function code? Because (1/x )+(1/x) is not equal to 1/2x..

    and the function is something like this:

     

    Tuesday, December 21, 2010 9:22 AM
  • You can replace the calculation by any formula, the logic remains same..

     


    Happy to Help :)
    Tuesday, December 21, 2010 9:31 AM
  • In your post you do :

     eq = (1 / sum1) + (1 / sum2) + (1 / sum3) + (1 / sum4) + (1 / sum5)

    which is not  what your formula says. Ct = (1 / (1/c1+..))


    Happy to Help :)
    Tuesday, December 21, 2010 9:35 AM
  • just for the record both these produce same results.

     

                double c1 = 0.1;
                double c2 = 0.2;
                double c3 = 0.3;
                double c4 = 0.4;
                double c5 = 0.5;

                double C = (1 / c1) + (1 / c2) + (1 / c3) + (1 / c4) + (1 / c5);

                List<double> listC = new List<double>();
                listC.Add(c1);
                listC.Add(c2);
                listC.Add(c3);
                listC.Add(c4);
                listC.Add(c5);

                double Clist = 0.0;

                for (int i = 0; i < 5; i++)
                {
                    Clist += (1 / listC[i]);
                }

     

    C and Clist are same, so sum +=   (1 / ListBox1.Items(index)) should give same result as

    eq = (1 / sum1) + (1 / sum2) + (1 / sum3) + (1 / sum4) + (1 / sum5)

    Happy to Help :)
    Tuesday, December 21, 2010 9:45 AM
  • vijaykamat, 

    Because you asked about something which was not asked, maybe can we put more rubish in this thread.

    Do you know if it is snowing at the coast of Congo in Africa?

    In my idea was your first answer enough. 

    (And try to use threaded view as most regulars to the forums do).

     


    Success
    Cor
    Tuesday, December 21, 2010 9:49 AM
  • Dear Cor Lighert,

    I am sorry but I do not see any rubish in this thread.

    I am sorry if I posted unnecessarily, but I thought it was good to clear out the doubts which were raised on the calculation part.

    Next time I will try to stick to the point!


    Happy to Help :)
    Tuesday, December 21, 2010 9:55 AM
  • OK, that helps. I couldn't tell from your code what you were trying to do, especially the use of ListBox2.SelectedIndex

    try this - it will work no matter how many values are in the ListBox

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
        Dim eq As Double = 0
    
        If ComboBox1.SelectedIndex = 0 Then
          'Calculating Equivalent Capacitance in Series
    
          For Each d As Double In ListBox2.Items
            eq += 1 / d
          Next
    
        Else
          'Calculating Equivalent Capacitance in Parallel
    
          For Each d As Double In ListBox2.Items
            eq += d
          Next
    
        End If
    
        CTB2.Text = Format(eq, "0.00")
        
      End Sub
    
    • Marked as answer by SouLReaPeR1992 Tuesday, December 21, 2010 12:37 PM
    Tuesday, December 21, 2010 11:15 AM
  • Thanks for every assistance provided, I have done my coding sucessfully!

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sum As Double
        Dim eq As Double = 0
    
        If ComboBox1.SelectedIndex = 0 Then
          'Finding Equivalent Capacitance in Series
          For Each d As Double In ListBox2.Items
            eq += 1 / d
          Next
          sum = 1 / eq
        ElseIf ComboBox1.SelectedIndex = 1 Then
          'Finding Equivalent Capacitance in Parallel
          For Each d As Double In ListBox2.Items
            eq += d
            sum = eq
          Next
        End If
        
        CTB2.Text = Format(sum, "0.00")
      End Sub

     


    Nothing's gonna change my love for you <3
    Tuesday, December 21, 2010 12:40 PM