Been trying to figure out how to do this for a while, but I can't seem to find anything that exactly covers what I'm trying to do. I'm really a novice when it comes to vb .net, so this might be staring me in the face and I wouldn't know it.

I've got a Motorola MC3090 mobile barcode scanner running Windows CE 5.0 at work. I wrote a small program (in .net 3.5) that allows me to scan barcodes and then get counts for specific barcodes when I type all or part of the barcode into an textbox. That works pretty well for receiving, but I need something that works better for the reverse process (shipping things out).

I have an textbox which accepts the barcodes. When the scanner emulates an enter key press, it adds the code to a listbox and clears the input field. This process loops as many times as my quantity field specifies. There's another textbox which I use to input partial barcodes that I want counts for. It looks for strings in the listbox that match, dumps them into a second hidden listbox, and counts those.

If you're the tl;dr type, here's the short of what I want to do with all of the above. I want to take the contents of the visible listbox, summarize by listing each unique barcode, and have a count of how many of each unique barcode appears in the listbox.

tl;dr?

How about this

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim words() As String = Split("Some luck lies in not getting what you thought you wanted but getting what you have which once you have got it you may be smart enough to see is what you would have wanted had you known")

        For Each word As String In words
            ListBox1.Items.Add(word)
        Next

    End Sub

    Private Sub btnScan_Click(sender As System.Object, e As System.EventArgs) Handles btnScan.Click

        Dim unique As New Dictionary(Of String, Integer)

        For Each item As String In ListBox1.Items
            If unique.ContainsKey(item) Then
                unique(item) += 1
            Else
                unique(item) = 1
            End If
        Next

        For Each key As String In unique.Keys
            ListBox2.Items.Add(key & " (" & unique(key) & ")")
        Next

    End Sub

End Class

I set the Sorted property on ListBox2 to True.

commented: worked great +0

Thanks! With a few modifications to fit it with my program it works beautifully. I've still got a few usability issues to work out, but it is functional. Here's the full code if anyone is interested. This could just as easily work as a desktop application.

Public Class frmMainForm

    Private Sub txtBarcode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBarcode.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then

            For a = 1 To txtQuantity.Value

                lstBarcodes.Items.Add(txtBarcode.Text)

            Next

            txtBarcode.Text = String.Empty
            lblTotal.Text = lstBarcodes.Items.Count

        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        If lstSummary.Visible = True Then
            lstSummary.Visible = False
            lstSummary.Items.Clear()
            lstBarcodes.Items.Clear()
            lblCount.Text = String.Empty
            lblTotal.Text = String.Empty
            btnDelete.Enabled = True
        Else
            lstBarcodes.Items.Clear()
            lblCount.Text = String.Empty
            lblTotal.Text = String.Empty
        End If

    End Sub

    Private Sub txtCount_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCount.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then

            lstResults.Items.Clear()
            lblCount.Text = String.Empty

            For Each Item As String In lstBarcodes.Items

                If Item.Contains(txtCount.Text) Then

                    lstResults.Items.Add(Item)

                End If

            Next

            lblCount.Text = lstResults.Items.Count

        End If
    End Sub

    Private Sub txtCount_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCount.TextChanged
        lblCount.Text = String.Empty
    End Sub

    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        lstBarcodes.Items.Remove(lstBarcodes.SelectedItem)
        lblTotal.Text = lstBarcodes.Items.Count

    End Sub

    Private Sub FocusBarcodeEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtQuantity.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then

            txtBarcode.Focus()

        End If
    End Sub

    Private Sub btnSummary_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSummary.Click
        If lstSummary.Visible = False Then
            lstSummary.Visible = True
            btnDelete.Enabled = False

            Dim unique As New Dictionary(Of String, Integer)

            For Each item As String In lstBarcodes.Items
                If unique.ContainsKey(item) Then
                    unique(item) += 1
                Else
                    unique(item) = 1
                End If
            Next

            For Each key As String In unique.Keys
                lstSummary.Items.Add(key & " (" & unique(key) & ")")
            Next
        Else

            lstSummary.Visible = False
            btnDelete.Enabled = True
            lstSummary.Items.Clear()
        End If
    End Sub
End Class

tl;dr?

Sorry, been hanging out in engadget comments too much. Stands for "too long; didn't read."

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.