how to add a specific value from a list box in vb.net?

2.3k views Asked by At

enter image description here

how can i retrieve the total price only from this listbox and then total up in a textbox?? this is the code to add data to the listbox

Private Sub cbitem_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbitem.SelectedIndexChanged
    If cbitem.SelectedIndex < 0 Then Return
    Dim price = Convert.ToDecimal(items(cbitem.SelectedIndex, 1))
    txtitemprice.Text = price
End Sub

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
    Dim price = Convert.ToDecimal(txtitemprice.Text)
    Dim quantity = Convert.ToInt32(txtquantity.Text)
    Dim totalprice As Integer = price * quantity
    lstorder.Items.Add(cbitem.SelectedItem & vbTab & vbTab & vbTab & price & vbTab & vbTab & vbTab & quantity & vbTab & vbTab & totalprice)
End Sub
1

There are 1 answers

0
nbadaud On

Suppose that you have two controls : ListBox1 and TextBox1.

' Initialise the textBox to 0    
TextBox1.Text = 0

' Insert values in your listbox
 ListBox1.Items.Add(1)
 ListBox1.Items.Add(2)
 ListBox1.Items.Add(3)
 ListBox1.Items.Add(4)

 ' For each item in your ListBox
 For i As Integer = 0 To ListBox1.Items.Count - 1
      TextBox1.Text = TextBox1.Text + ListBox1.Items(i)
 Next

For your work, you have to get the price of the current item and multiply it with it's quantity. Or you can sum each item of the totalPrice column.