如何从vb.net中的列表框中添加特定值?

时间:2016-12-22 14:11:29

标签: vb.net listbox listboxitems

enter image description here

我怎样才能从这个列表框中检索总价格,然后在文本框中总计? 这是将数据添加到列表框的代码

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 个答案:

答案 0 :(得分:0)

假设您有两个控件:ListBox1和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

对于您的工作,您必须获得当前项目的价格并将其乘以其数量。或者,您可以对totalPrice列的每个项目求和。