# How to Find 3 digit numbers

### Question

• Hi All,

Want to do;

I have 3 digit of number in TextBox1 to find in Listbox1.Example 240 to 249 and 250 to 259.

```Option Strict On
Option Explicit On
Option Infer Off

Imports System.IO
Imports System.Data
Imports System.Text

Public Class Form1

For I As Integer = 24 To 25
For J As Integer = 0 To 9
TextBox1.Text &= I.ToString & J.ToString & vbCrLf
Next
Next

With ListBox1
End With
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim StringToCheck As New StringBuilder() ' ****
For I As Integer = 0 To ListBox1.Items.Count - 1
StringToCheck.AppendLine(ListBox1.Items(I).ToString) ' ****
Next I
StringToCheck = StringToCheck.Replace(vbCrLf, ",").Remove(StringToCheck.Length - 1, 1)

Dim StringsToCheck As String() = StringToCheck.ToString.Split(","c) ' ****

Dim Find2 As String = TextBox1.Text

Dim Count2 As Integer = 0
For Each Check2 As String In StringsToCheck
If Check2.Contains(Find2(0)) AndAlso Check2.Contains(Find2(1)) AndAlso Check2.Contains(Find2(2)) Then Count2 += 1
Next
TextBox3.AppendText("Occurances ( " & Find2 & ")= " & Count2.ToString & vbCrLf)

End Sub
End Class
```

Saturday, April 8, 2017 2:55 PM

• That code only count digit 240 not count all the orther 241,242...249.

Because you are only searching with the first three characters from the text box: 2, 4 and 0.

```  If Check2.Contains(Find2(0)) AndAlso Check2.Contains(Find2(1)) AndAlso Check2.Contains(Find2(2)) Then Count2 += 1
```

Do not use a text box as the source of your strings to find - create a list of strings to find using the code provided in your previous question.

```    Private StringsToFind As New List(Of String)

For I As Integer = 24 To 25
For J As Integer = 0 To 9
TextBox1.Text &= I.ToString & J.ToString & vbCrLf
Next
Next```

Then iterate over that list, repeating the search for each item in the list.

```        Dim Count2 As Integer = 0
For Each Find2 As String In StringsToFind
For Each Check2 As String In StringsToCheck
If Check2.Contains(Find2(0)) AndAlso Check2.Contains(Find2(1)) AndAlso Check2.Contains(Find2(2)) Then
Count2 += 1
TextBox3.AppendText("Occurances ( " & Find2 & ")= " & Count2.ToString & vbCrLf)
End If
Next
Next```

Textboxes are for displaying results and getting input from the user - they are not for storing the data that you want to work with.

• Marked as answer by Sunday, April 9, 2017 3:29 AM
Sunday, April 9, 2017 2:57 AM

### All replies

• It is not clear how you want to find the three digit number within the four digit numbers in the ListBox.  The following example assumes a direct numeric comparison, but if you want to test sub strings of digits you could easily modify the code to work with strings and perform multiple sub string comparisons.

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

For I As Integer = 24 To 25
For J As Integer = 0 To 9
TextBox1.Text &= I.ToString & J.ToString & vbCrLf
Next
Next

With ListBox1
End With
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'parse all ListBox strings into a list of individual items (integer seems appropriate)
Dim itemsList As New List(Of Integer)
For i As Integer = 0 To ListBox1.Items.Count - 1
itemsList.AddRange(From s In ListBox1.Items(i).ToString.Split(","c) Select Integer.Parse(s))
Next
'parse all search text into a dictionary of items to search for and the count of items found
Dim itemOccurances As New Dictionary(Of Integer, Integer)
For Each line As String In TextBox1.Lines
If String.IsNullOrEmpty(line) Then Continue For
Next
'count the of times an item occurs in the list (use whatever comparison is appropriate)
For Each item As Integer In itemsList
If itemOccurances.ContainsKey(item) Then itemOccurances(item) += 1
Next
'display the results in the output textbox
For Each itemOccurance As KeyValuePair(Of Integer, Integer) In itemOccurances
TextBox3.AppendText("Occurances ( " & itemOccurance.Key.ToString & ")= " & itemOccurance.Value.ToString & vbCrLf)
Next
End Sub```

Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

Saturday, April 8, 2017 3:28 PM
• Thank Reed Kimble,

Example 3 digit number in textbox 240 .what I mean it a combination number.(240=402=024=042=420=204)

The result is "Occrances 240=4)

Saturday, April 8, 2017 3:37 PM
• Hi

You do not explain fully what you want.

If the textbox contains 240 then show the matches from the listbox items you are expecting, and, show why/how those matches are actual reached.

Regards Les, Livingston, Scotland

Saturday, April 8, 2017 4:11 PM
• So you also need to calculate the permutations of entries in the textbox and add all of those to the criteria list. I guess you'll also want to stick with strings and use the Contains() method to see if the 4 digit numeric string contains any of the three digit numeric strings at any position. That's just a minor expansion and modification to the example I posted. See if you can work it out and then post back with your attempt if you run into problems.

Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

Saturday, April 8, 2017 7:07 PM
• Example 3 digit number in textbox 240 .what I mean it a combination number.(240=402=024=042=420=204)

The result is "Occrances 240=4)

Why is the result 4?  You should show how you have calculated that 4 is the correct result.
Saturday, April 8, 2017 9:20 PM
• Why is the result 4?  You should show how you have calculated that 4 is the correct result.
Agreed. I modified the sample to use the permutations of the number and only found 2 matches, not 4.  A visual scan of the numbers seems to confirm this.

Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

Saturday, April 8, 2017 10:49 PM
• Hi Leshay,

If we look in Listbox1,we can found

4702
2204
0294
0424

total All is 4.

Saturday, April 8, 2017 11:43 PM
• Hi Acamar;

```  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim StringToCheck As New StringBuilder() ' ****
For I As Integer = 0 To ListBox1.Items.Count - 1
StringToCheck.AppendLine(ListBox1.Items(I).ToString) ' ****
Next I
StringToCheck = StringToCheck.Replace(vbCrLf, ",").Remove(StringToCheck.Length - 1, 1)

Dim StringsToCheck As String() = StringToCheck.ToString.Split(","c) ' ****

Dim A As String = TextBox1.Text

Dim Search As String() = {A(0) & A(1) & A(2)}

Dim Count2 As Integer = 0

For Each Find2 As String In Search
Count2 = 0
For Each Check2 As String In StringsToCheck
Select Case (Find2.Count)
Case 2
If Check2.Contains(Find2(0)) AndAlso Check2.Contains(Find2(1)) Then Count2 += 1
Case 3
If Check2.Contains(Find2(0)) AndAlso Check2.Contains(Find2(1)) AndAlso Check2.Contains(Find2(2)) Then Count2 += 1
End Select
Next
TextBox3.AppendText("Occurances ( " & Find2 & ")= " & Count2.ToString & vbCrLf)

Next

End Sub```

Saturday, April 8, 2017 11:45 PM
• If we look in Listbox1,we can found

4702
2204
0294
0424

total All is 4.

You have listed 4 items from the listbox.   Why did you select these four items, and ignore all the others?
Saturday, April 8, 2017 11:47 PM
• ```        For Each Find2 As String In Search
Count2 = 0
For Each Check2 As String In StringsToCheck
Select Case (Find2.Count)
Case 2
If Check2.Contains(Find2(0)) AndAlso Check2.Contains(Find2(1)) Then Count2 += 1
Case 3
If Check2.Contains(Find2(0)) AndAlso Check2.Contains(Find2(1)) AndAlso Check2.Contains(Find2(2)) Then Count2 += 1
End Select
Next
TextBox3.AppendText("Occurances ( " & Find2 & ")= " & Count2.ToString & vbCrLf)

Next
```

Do you mean that you have found 4 items, but you don't know which ones are the ones that you found?   It is very difficult when you force people to guess at what you are trying to do.

```  Dim FoundList as New List(Of string)
For Each Find2 As String In Search
Count2 = 0
For Each Check2 As String In StringsToCheck
Select Case (Find2.Count)
Case 2
If Check2.Contains(Find2(0)) AndAlso Check2.Contains(Find2(1)) Then
Count2 += 1
End If
Case 3
If Check2.Contains(Find2(0)) AndAlso Check2.Contains(Find2(1)) AndAlso Check2.Contains(Find2(2)) Then
Count2 += 1
End If
End Select
Next
TextBox3.AppendText("Occurances ( " & Find2 & ")= " & Count2.ToString & vbCrLf)
Next```
You haven't indicated what you want to do with that list when you find it - you could display it in a textbox or a list box.

Saturday, April 8, 2017 11:57 PM
• Because, I want to take the combination digit 240 only in Listbox.

Thank .

Sunday, April 9, 2017 12:06 AM
• Because, I want to take the combination digit 240 only in Listbox.

That is not sufficient description.  When I asked you to provide a description, you just posted a different version of the code that does the same thing.  That's not helpful.  It is possible to see what the code is doing (although with two different versions, it is very confusing).  The important bit is this:

```    For Each Check2 As String In StringsToCheck
If Check2.Contains(Find2(0)) AndAlso Check2.Contains(Find2(1)) AndAlso Check2.Contains(Find2(2)) Then Count2 += 1
Next```

which is how you get your count of 4.  You have now indicated that 4 is correct.   So if that code is giving you the right answer, what exactly is it that you want to put into the listbox?  If it is the four items that were found, then that's what the code I provided does.  If it's something else, you need to say what it is.

Why not tell us what the listbox name is, and avoid the need for people to guess?

Sunday, April 9, 2017 12:22 AM
• Sorry Acamar,Maybe my question not good enough.

If use  your code provide the count 240 is correct.My question is "Why that code NOT loop to the end of Textbox1.

That code only count digit 240 not count all the orther 241,242...249.

Sunday, April 9, 2017 2:13 AM
• That code only count digit 240 not count all the orther 241,242...249.

Because you are only searching with the first three characters from the text box: 2, 4 and 0.

```  If Check2.Contains(Find2(0)) AndAlso Check2.Contains(Find2(1)) AndAlso Check2.Contains(Find2(2)) Then Count2 += 1
```

Do not use a text box as the source of your strings to find - create a list of strings to find using the code provided in your previous question.

```    Private StringsToFind As New List(Of String)

For I As Integer = 24 To 25
For J As Integer = 0 To 9
TextBox1.Text &= I.ToString & J.ToString & vbCrLf
Next
Next```

Then iterate over that list, repeating the search for each item in the list.

```        Dim Count2 As Integer = 0
For Each Find2 As String In StringsToFind
For Each Check2 As String In StringsToCheck
If Check2.Contains(Find2(0)) AndAlso Check2.Contains(Find2(1)) AndAlso Check2.Contains(Find2(2)) Then
Count2 += 1
TextBox3.AppendText("Occurances ( " & Find2 & ")= " & Count2.ToString & vbCrLf)
End If
Next
Next```

Textboxes are for displaying results and getting input from the user - they are not for storing the data that you want to work with.

• Marked as answer by Sunday, April 9, 2017 3:29 AM
Sunday, April 9, 2017 2:57 AM
• Thank you very very much;
Sunday, April 9, 2017 3:29 AM