Results 1 to 8 of 8

Thread: Counting digits from multiple textbox in VB6

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    35

    Counting digits from multiple textbox in VB6

    need to count or add the number of same digits in a textbox.result from textbox1, textbox2, textbox3, and textbox4. In other words if the user enters "9" in textbox1, "9" in textbox2, "4" in textbox3, and "9" in textbox4, I need to obtain the value of "3" in textbox.result because 9 repeats 3 times. Can someone help me? So far I tried this but doesn't seem to work as expected.


    Dim t1 = "9"
    Dim t2 = "9"
    Dim t3 = "4"
    Dim t4 = "9"

    Dim result As String = {t1, t2, t3, t4}.GroupBy(Function(t) t).OrderByDescending(Function(g) g.Count).First.Count.ToString

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,409

    Re: Counting digits from multiple textbox in VB6

    Looks like you are using a .Net version of Visual Basic, not Visual Basic/Studio 6.0. I'll advice the moderators to move this thread to its appropriate part of this forum. But, if you want a VB6 solution, will gladly guide you in that direction.

    Sam

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,409

    Re: Counting digits from multiple textbox in VB6

    Looking at your other threads, I DO believe you ARE using a .Net product, but not VB6...but, if you want a "VB6" start...look at these...the first one will see if a given string (Typed into text2) is found ANYWHERE within the textboxes (0 through 5), while, the second one will find exact matches...they both use an ARRAY of textboxes:

    Code:
    Private Sub Command1_Click()    Dim iLoop As Integer
        Dim numOccurences As Integer
        Dim z As Integer
        For iLoop = 0 To Text1.Count - 1
            z = InStr(1, Text1(iLoop).Text, Text2.Text)
            If z > 0 Then
                numOccurences = numOccurences + 1
            End If
        Next iLoop
        Debug.Print CStr(numOccurences)
    End Sub
    Code:
    Private Sub Command2_Click()
        Dim iLoop As Integer
        Dim numOccurences As Integer
        For iLoop = 0 To Text1.Count - 1
            If Text1(iLoop).Text = Text2.Text Then
                numOccurences = numOccurences + 1
            End If
        Next iLoop
        Debug.Print CStr(numOccurences)
    End Sub

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,921

    Re: Counting digits from multiple textbox in VB6

    Here's another way to express that in VB6 (assuming t1 to t4 are textboxes on your VB6-Form):
    Code:
    Result = UBound(Filter(Array(t1, t2, t3, t4), "9")) + 1
    Olaf

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,409

    Re: Counting digits from multiple textbox in VB6

    difference between amateur and professional!

  6. #6
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,019

    Re: Counting digits from multiple textbox in VB6

    Quote Originally Posted by SamOscarBrown View Post
    difference between amateur and professional!
    It is succinct indeed but alas, this is not what the LINQ expr in OP does.

    Here is something closer to the original query

    thinBasic Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim vElem       As Variant
    5.     Dim lMax        As Long
    6.    
    7.     With CreateObject("Scripting.Dictionary")
    8.         For Each vElem In Array(Text1, Text2, Text3, Text4)
    9.             .Item(vElem.Text) = .Item(vElem.Text) + 1
    10.         Next
    11.         For Each vElem In .Items
    12.             If vElem > lMax Then
    13.                 lMax = vElem
    14.             End If
    15.         Next
    16.     End With
    17.     Debug.Print lMax
    18. End Sub

    cheers,
    </wqw>

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,023

    Re: Counting digits from multiple textbox in VB6

    Thread moved to .NET from Classic VB, since the OP used LINQ, which is .NET only.
    My usual boring signature: Nothing

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,223

    Re: Counting digits from multiple textbox in VB6

    Try this. slight change... Assuming there will always be Integers typed in the textboxes

    Code:
    Public Class Form1
    
        Dim textboxes() As TextBox
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            textboxes = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4}
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim x As Integer
            Dim result As String = textboxes.GroupBy(Function(t) If(Integer.TryParse(t.Text, x), x, -1)).OrderByDescending(Function(g) g.Count).First.Count.ToString
            MsgBox(result)
        End Sub
    
    End Class

Tags for this Thread

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