Click here to Skip to main content
16,004,653 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i'm using visual studio 2015,window form app with VB code
i have a list of values in the listbox,so what i want is when the values(multiple values) selected,it adds the values & display in textbox
Posted
Updated 20-Nov-15 3:23am
v3
Comments
phil.o 20-Nov-15 9:24am    
What have you tried so far?
Patrice T 20-Nov-15 13:07pm    
What have done so far ? Where are you stuck ?

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

I'll give you a hint: look at int.TryParse (or double.TryParse for floating point inputs) and it will let you convert user entered values to a number so you can add them. You can then use ToString or String.Format to convert the value back to a string for display.
 
Share this answer
 
v2
C#
int n = 0, i;
foreach (var item in listBox1.SelectedItems)
{
    bool result = Int32.TryParse(item, out i);
    if(result)
    {
        n += i;
    }
}
txtBox.Text = n + "";

Not tested, but should work :)

Update - VB .net code
C#
Dim n As Integer = 0, i As Integer
For Each item As var In listBox1.SelectedItems
	Dim result As Boolean = Int32.TryParse(item, i)
	If result Then
		n += i
	End If
Next
txtBox.Text = n + ""



-KR
 
Share this answer
 
v2
Comments
Maciej Los 20-Nov-15 11:39am    
Tag is: vb.net, not c#.
Something like that:
VB
TextBox.Text = ListBox1.SelectedItems.Cast(Of Double)().Sum().ToString()

should work as well!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900