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
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
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
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
Re: Counting digits from multiple textbox in VB6
difference between amateur and professional!
Re: Counting digits from multiple textbox in VB6
Quote:
Originally Posted by
SamOscarBrown
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:
Option Explicit
Private Sub Form_Load()
Dim vElem As Variant
Dim lMax As Long
With CreateObject("Scripting.Dictionary")
For Each vElem In Array(Text1, Text2, Text3, Text4)
.Item(vElem.Text) = .Item(vElem.Text) + 1
Next
For Each vElem In .Items
If vElem > lMax Then
lMax = vElem
End If
Next
End With
Debug.Print lMax
End Sub
cheers,
</wqw>
Re: Counting digits from multiple textbox in VB6
Thread moved to .NET from Classic VB, since the OP used LINQ, which is .NET only.
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