# Thread: Count the number of different values in 8 textbox

1. ## Count the number of different values in 8 textbox

Registered forum members do not see this ad.

Hello everyone.

I am developing a project and I would create a command that scrolls the values of 8 textboxes and tell me how many different values among them there are.

For example, if the values are (1 - 2 - 2 - 1 - 3 - 5 - 1 - 3) the msgbox should say "4" because the values different from each other are (1,2,3,5).

Should be inserted into a more complex project, but for now I created a simple project to study the problem, but I have several problems.

I created 8 TextBoxes and a CommandButton.

This is the code of the commandbutton

Code:
```Private Sub CommandButton1_Click()
Me.Hide

Dim txt(1 To 8) As Double

txt(1) = TextBox1.Text
txt(2) = TextBox2.Text
txt(3) = TextBox3.Text
txt(4) = TextBox4.Text
txt(5) = TextBox5.Text
txt(6) = TextBox6.Text
txt(7) = TextBox7.Text
txt(8) = TextBox8.Text

Dim I As Integer
Dim J As Integer
Dim contatore As Variant

contatore = 1

For J = 1 To 8
For I = J To 8
If txt(J) <> txt(I) Then
contatore = contatore + 1
Else
contatore = contatore
End If
Next I
Next J
MsgBox contatore, vbOKOnly, "contatore"

Me.Show

End Sub```
The problem is : it counts how many times the values are different from each other and not how many values are different.

I hope someone can help me or even recommend a way

2. You need to step through the list of texts (one-at-a-time), and store the individual numbers to a variable list, *IF* that number does not already belong to the variable list.

Then report the value of the variable list, the count of different numbers, etc..

Hope this helps!

3. Originally Posted by RenderMan
You need to step through the list of texts (one-at-a-time), and store the individual numbers to a variable list, *IF* that number does not already belong to the variable list.

Then report the value of the variable list, the count of different numbers, etc..

Hope this helps!
For sure!

Thank you very much

I learned to use the collection object, and I wrote this.

Code:
```Private Sub CommandButton1_Click()
Me.Hide

Dim txt(1 To 8) As Double
Dim database As New Collection

txt(1) = TextBox1.Text
txt(2) = TextBox2.Text
txt(3) = TextBox3.Text
txt(4) = TextBox4.Text
txt(5) = TextBox5.Text
txt(6) = TextBox6.Text
txt(7) = TextBox7.Text
txt(8) = TextBox8.Text

Dim I As Integer
Dim J As Integer

For J = 1 To 8
For I = 1 To 8
If txt(J) <> txt(I) Then
If Find(database, txt(J)) Then
Else
End If
Else
End If
Next I
Next J

MsgBox database.Count, vbOKOnly, "contatore"

Me.Show
End Sub

Function Find(database As Collection, valore As Double) As Boolean
For k = 1 To database.Count
If database.Item(k) = valore Then
Find = True
Exit For
End If
Next
End Function```
Now it works!

4. Originally Posted by lubracali
Thank you very much

You're welcome.

5. Another thing you can do is most .Net collections have a contains method thats returns a boolean

This iterates through all the textboxes and adds only if it is not already in the collection

Code:
``` Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim lst As New List(Of Integer)

For Each ctrl As Control In Me.Controls

If TypeOf ctrl Is TextBox Then

If Not lst.Contains(ctrl.Text) Then
End If

End If

Next

For Each element In lst
MsgBox(element)
Next

End Sub```

6. Originally Posted by Jeff H
Another thing you can do is most .Net collections have a contains method thats returns a boolean

This iterates through all the textboxes and adds only if it is not already in the collection

Code:
``` Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim lst As New List(Of Integer)

For Each ctrl As Control In Me.Controls

If TypeOf ctrl Is TextBox Then

If Not lst.Contains(ctrl.Text) Then
End If

End If

Next

For Each element In lst
MsgBox(element)
Next

End Sub```
I tried this code, but the only element that answers to the method "contains" is the listbox.

I think it is due to vba in autocad, which is different from vb.net

7. Registered forum members do not see this ad.

VBA could use the Windows.Scripting.Dictionary object. That has a similar Dictionary.Exists to test if it is already present.

Though, it may be easier to just take RenderMan’s suggestion, or continue with Collections. I haven’t used Windows.Scripting.Dictionary in a VBA project but I doubt it would have much better performance over a custom “Exist” routine.

Dictionary objects do benefit from additional storage. If TextBox.Text were set as the Key, then Value could be incremented to keep track of the number of times it appears.

#### Posting Permissions

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