Answered by:
Sum up or using functions of the values in ListBox

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.
Thanks.'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
Question
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))
NextYou can limit it to first 5 items also:
For index As Integer = 0 To 5
Is this what you are looking for?
sum += (1 / ListBox1.Items(index))
Next
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

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
All replies


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))
NextYou can limit it to first 5 items also:
For index As Integer = 0 To 5
Is this what you are looking for?
sum += (1 / ListBox1.Items(index))
Next
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

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.




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 :) 
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 
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 :) 
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

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