i somehow got the gist of adding items and filtering the already inputted ones but i cant seem to know how to start a loop where the code searches for similar items in the listbox before adding new items. when trying to run my code, it does filter out the similar items yet it still add info/items in the listbox. below is a a part of my code.

If counter = 1 Then
                lstboxProd.Items.Add(cmbBoxSel.Text)
                lstboxPrice.Items.Add(txtPrice.Text)
                lstboxQty.Items.Add(txtQuant.Text)
                lstboxTotal.Items.Add(txtPrice.Text * txtQuant.Text)
                counter = counter - 1
            Else
                For index As Integer = 0 To lstboxProd.Items.Count - 1
                    If cmbBoxSel.Text = lstboxProd.Items.Item(index) Then
                        MsgBox("Similar item on list")
                    Else
                        lstboxProd.Items.Add(cmbBoxSel.Text)
                        lstboxPrice.Items.Add(txtPrice.Text)
                        lstboxQty.Items.Add(txtQuant.Text)
                        lstboxTotal.Items.Add(txtPrice.Text * txtQuant.Text)
                    End If
                Next index
            End If

    End If

FuLL CODE:

 Public Class frmMain

    Private Sub cmbBoxSelect_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBoxSel.SelectedIndexChanged

        If cmbBoxSel.Text = "Pepperoni Pizza" Then
            txtPrice.Text = "250.00"
        ElseIf cmbBoxSel.Text = "Hawaiian Pizza" Then
            txtPrice.Text = "260.00"
        ElseIf cmbBoxSel.Text = "Bacon & Cheese Pizaa" Then
            txtPrice.Text = "280.00"
        ElseIf cmbBoxSel.Text = "Mushroom Pizza" Then
            txtPrice.Text = "150.00"
        ElseIf cmbBoxSel.Text = "Sausage Pizza" Then
            txtPrice.Text = "300.00"
        End If

        txtQuant.Enabled = True

    End Sub

    Dim counter As Integer = 1
    Dim checker As Boolean = False

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If txtQuant.Text = "" Then
            MsgBox("Please fill up needed information")
        Else
            If counter = 1 Then
                lstboxProd.Items.Add(cmbBoxSel.Text)
                lstboxPrice.Items.Add(txtPrice.Text)
                lstboxQty.Items.Add(txtQuant.Text)
                lstboxTotal.Items.Add(txtPrice.Text * txtQuant.Text)
                counter = counter - 1
            Else
                For index As Integer = 0 To lstboxProd.Items.Count - 1
                    If cmbBoxSel.Text = lstboxProd.Items.Item(index) Then
                        MsgBox("Similar item on list")
                    Else
                        lstboxProd.Items.Add(cmbBoxSel.Text)
                        lstboxPrice.Items.Add(txtPrice.Text)
                        lstboxQty.Items.Add(txtQuant.Text)
                        lstboxTotal.Items.Add(txtPrice.Text * txtQuant.Text)
                    End If
                Next index
            End If
        End If

            txtQuant.Text = ""

            btnSave.Enabled = True
            btnRemove.Enabled = True
            btnClear.Enabled = True

    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click

        If MsgBox("Do you want to remove?", vbYesNo) = vbYes Then
            lstboxProd.Items.Remove(lstboxProd.SelectedItem)
            lstboxPrice.Items.RemoveAt(lstboxProd.SelectedIndex + 1)
            lstboxQty.Items.RemoveAt(lstboxProd.SelectedIndex + 1)
            lstboxTotal.Items.RemoveAt(lstboxProd.SelectedIndex + 1)

            counter = 1
        End If

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        If MsgBox("Clear All Items?", vbYesNo) = vbYes Then
            lstboxPrice.Items.Clear()
            lstboxProd.Items.Clear()
            lstboxQty.Items.Clear()
            lstboxTotal.Items.Clear()

            counter = 1
        End If

    End Sub

    Private Sub txtQuant_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtQuant.KeyPress

        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                e.Handled = True
            End If
        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If MsgBox("Save?", vbYesNo) = vbYes Then
            lstboxPrice.Items.Clear()
            lstboxProd.Items.Clear()
            lstboxQty.Items.Clear()
            lstboxTotal.Items.Clear()

            MsgBox("Transaction saved!")

            counter = 1
        End If
    End Sub

    Dim total As Integer = 0

    Private Sub lstboxTotal_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstboxTotal.SelectedIndexChanged
        For lstIndex As Integer = 0 To lstboxTotal.Items.Count
            total = total + lstboxTotal.Items.Item(lstIndex)
        Next lstIndex
        lblTotalP.Text = total
    End Sub
End Class

You should check first if the list contains the item you want to add and then decide what to do. You might want to exit the sub after line 37. Also make sure to post in the correct forum. Vb.net?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.