Results 1 to 5 of 5

Thread: [RESOLVED] Count Duplicate entries in TextBoxes

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Resolved [RESOLVED] Count Duplicate entries in TextBoxes

    Hello.

    I have 50 Texboxes.

    I need to count the number of duplicate entries, and provide a summary (count) for each product code. This makes it easier when the order needs to be placed, and the user don't have to count each value manually.

    Any idea on how to do this. I have tried looping through all 50 boxes to no avail. This seemed easy, but, clearly I am to thick to think of a solution.

    Any help will be appreciated.

  2. #2
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Count Duplicate entries in TextBoxes

    Uhhh this is the only solution that popped to my head when I looked at it. No doubt there are probably cleaner methods:

    vb Code:
    1. Dim totalrep as int32 = 0
    2. For each tb As TextBox In Me.Controls
    3.    Dim myText As String = tb.Text
    4.    For each txtBox As TextBox In Me.Controls
    5.        if txtBox.Text = myText then
    6.          totalrep += 1
    7.        End If
    8.    Next
    9. Next

    Only glaring problem I see is duplicate counts (i.e TextBox2.Text = TextBox1.Text and TextBox1.Text = TextBox2.text) which would result in double results. So yeah, I guess you'd have to figure out how to fix that (I'm not sure if dividing the final result by 2 would do it...just a suggestion)

  3. #3
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Count Duplicate entries in TextBoxes

    Uhhh this is the only solution that popped to my head when I looked at it. No doubt there are probably cleaner methods:

    vb Code:
    1. Dim totalrep as int32 = 0
    2. For each tb As TextBox In Me.Controls.ofType(of textbox)
    3.    Dim myText As String = tb.Text
    4.    For each txtBox As TextBox In Me.Controls.oftype(of textbox)
    5.        if txtBox.Text = myText then
    6.          totalrep += 1
    7.        End If
    8.    Next
    9. Next

    Only glaring problem I see is duplicate counts (i.e TextBox2.Text = TextBox1.Text and TextBox1.Text = TextBox2.text) which would result in double results. So yeah, I guess you'd have to figure out how to fix that (I'm not sure if dividing the final result by 2 would do it...just a suggestion)

  4. #4
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Count Duplicate entries in TextBoxes

    may be you can try this:
    Code:
     Private Function GetProductSummary() As DataTable
            Dim dtSummary As DataTable
            Dim dcCol As DataColumn
            Dim txtBox As Control
            Try
                dtSummary = New DataTable("Order")
                dcCol = New DataColumn("ProductCode", Type.GetType("System.String"))
                dtSummary.Columns.Add(dcCol)
    
                dcCol = New DataColumn("Qty", Type.GetType("System.Int32"))
                dtSummary.Columns.Add(dcCol)
    
                For Each txtBox In Me.Controls
                    If txtBox.GetType.Name = "TextBox" Then
                        Dim drProducts() As DataRow
                        drProducts = dtSummary.Select("ProductCode = '" & txtBox.Text.Trim & "'")
                        If drProducts.Length > 0 Then
                            drProducts(0).Item("ProductCode") = txtBox.Text.Trim
                            drProducts(0).Item("Qty") += 1
                        Else
                            Dim drProduct As DataRow = dtSummary.NewRow
                            drProduct.Item("ProductCode") = txtBox.Text.Trim
                            drProduct.Item("Qty") = 1
                            dtSummary.Rows.Add(drProduct)
                        End If
                    End If
                Next
    
                Return dtSummary
            Catch ex As Exception
                Throw ex
            End Try
        End Function
    __________________
    Rate the posts that helped you

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: [RESOLVED] Count Duplicate entries in TextBoxes

    Thank you guys!!!!

    Doing it via a datatable never crossed my mind.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width