如何修复有关列表框的此逻辑错误

时间:2019-05-03 14:51:09

标签: vb.net listbox conditional

我想在vb.net中创建一个列表框,每当用户单击它更新信息的按钮时,该列表框就会列出该项目,我的变量是一个字符串和一个串联的整数,问题是当用户单击按钮,该项目不会从列表中删除

我尝试将整数变量转换为字符串,但仍然无法正常工作

Private Sub btnAddChicken_Click(sender As Object, e As EventArgs) Handles btnAddChicken.Click
        If cmbChicken.SelectedIndex = 0 Then
'''qfried is the quantity
'''tfried is the total amount
'''tfried and qfried are the variable i want to update
            qfried = qfried + 1
            tfried = tfried + pfried
            If ListAllOrders.Items.Contains("Fried Chicken - P" & Convert.ToString(tfried) & " Quantity (" & Convert.ToString(qfried) & ")") Then
                ListAllOrders.Items.Remove("Fried Chicken - P" & Convert.ToString(tfried) & " Quantity (" & Convert.ToString(qfried) & ")")
                ListAllOrders.Items.Add("Fried Chicken - P" & tfried & " Quantity - (" & qfried & ")")
                i += 1
            Else
                ListAllOrders.Items.Add("Fried Chicken - P" & tfried & " Quantity - (" & qfried & ")")
            End If

        End If
    End Sub

我希望信息会更新

1 个答案:

答案 0 :(得分:1)

ListBox的remove选项不适用于String,但适用于ListBoxItems。要删除它,您可以这样操作:

For Each item In ListAllOrders.Items
     If item.Contains(desiredStringToRemove) Then 
          ListAllOrders.Items.Remove(item)
          Exit for 
     End If
Next