Results 1 to 5 of 5

Thread: [RESOLVED] Loop thru textboxes to find missing digits

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2014
    Posts
    17

    Resolved [RESOLVED] Loop thru textboxes to find missing digits

    Ok. I'm stumped. I have a set of 10 textboxes.

    the first 8 boxes will store a digit from 0 to 9.

    the last two text boxes will be empty.

    My issue: in the last (2) textboxes I want to find the missing values that where not entered in boxes 1 thru 8.

    (EXAMPLE: if numbers 0 1 2 3 4 5 6 7, are listed in textboxes 1 thru 8 then the missing digits will be 8 & 9 to be displayed in the last two text boxes).

    I tried looping but was not able to get it right. Any suggestions? Nested loops? Any help would be greatly appreciated.

  2. #2
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: Loop thru textboxes to find missing digits

    'The following has no error detection yet.
    'Place on the Form, a control array of textboxes called txtNumbers with Indexes 0 to 9
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
     Dim arrNumbers(0 To 9) As Boolean   'As each number is found the equivalent element of this boolean array is set to True
     Dim i As Integer
      'Clear boxes 8 and 9 so user can keep trying other numbers
      txtNumbers(8).Text = ""
      txtNumbers(9).Text = ""
      'Loop through the first 8 boxes
      For i = 0 To 7
        arrNumbers(CInt(txtNumbers(i).Text)) = True  'Whatever number was typed, set the equivalent element to True
      Next i
      'Now loop through the boolean array elements to see which are still False (means that number was not typed by user)
      For i = 0 To UBound(arrNumbers)
        If arrNumbers(i) = False Then
            If txtNumbers(8).Text = "" Then
               'Place the first missing number (not typed) into box 8   because we had not placed anything in 8 yet
               txtNumbers(8).Text = i
            Else
               'Place the 2nd missing number (not typed) into box 9   because we had already placed a number into box 8
               txtNumbers(9).Text = i
            End If
        End If
      Next i
    End Sub

  3. #3
    Member
    Join Date
    Jan 2016
    Posts
    38

    Re: Loop thru textboxes to find missing digits

    I have a simple suggestion
    Let us keep the tags of the 2 empty text boxes as 0.

    Dim i, j as integer
    Dim sum as integer

    For i = 0 to 9
    Sum=0
    For j=0 to 7
    If textbox (j).text= i then
    sum=sum+1
    End If
    Next j

    If sum > 0 then
    If emptytext1.tag=0 then
    empty text1.tag=1
    empty text1.text=i
    else
    empty text2.text=i
    end if
    end if

    Next i


    Now make a loop that runs 10 times
    For each execution , let the loop concentrate on a specific number.
    Inside the loop make another nested loop that checks each text box for the specific number .
    If a number is absent,
    Put that to the required text box and change its tag to 1, if it's tag is 0 .

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Loop thru textboxes to find missing digits

    I think in this case, a collection may be the easiest way to go. Most of this code is just setting up data to work with. The important part is within the cmdCheck_Click event. Attached is a functional sample.

    Code:
    Private Sub cmdAddRandom_Click()
        ' Add test numbers to the textboxes
        AddRandomNumbers
    End Sub
    
    Private Sub ClearTextBoxes()
    Dim i As Integer
    
        For i = 0 To 9
            txtNumbers(i).Text = ""
        Next i
    End Sub
    
    Private Sub AddRandomNumbers()
    Dim col As Collection
    Dim i As Integer
    Dim r As Integer
    
        ' Clear all textboxes to start with
        ClearTextBoxes
    
        Set col = GetCollection
        
        For i = 0 To 7
            ' Get a random index for the collection
            r = Int(Rnd() * col.Count) + 1
            ' Add that collection item to the textbox
            txtNumbers(i).Text = col(r)
            ' Remove the collection item so it isn't reused
            col.Remove (r)
        Next i
        
        ' Cleanup
        Set col = Nothing
        
    End Sub
    
    Private Function GetCollection() As Collection
    Dim col As Collection
    Dim i As Integer
        
        Set col = New Collection
    
        ' Create a collection to initialize the textboxes
        For i = 0 To 9
            col.Add CStr(i), CStr(i)
        Next i
        
        Set GetCollection = col
        
        Set col = Nothing
    End Function
    
    Private Sub cmdCheck_Click()
    Dim i As Integer
    Dim col As Collection
    
        Set col = GetCollection
        
        ' Remove items from the collection that are found in the textboxes
        For i = 0 To 7
            col.Remove txtNumbers(i).Text
        Next i
        
        ' Deal with the results
        txtNumbers(8).BackColor = vbGreen
        txtNumbers(8).Text = col(1)
        
        txtNumbers(9).BackColor = vbGreen
        txtNumbers(9).Text = col(2)
        
        
        ' Cleanup
        Set col = Nothing
    End Sub
    
    Private Sub Form_Load()
        Randomize
    End Sub
    Attached Files Attached Files

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2014
    Posts
    17

    Re: Loop thru textboxes to find missing digits

    Thanks for all who replied. Great coding tips. I was close on the looping but could not get the values without errors! Looping vs collections, I think both where great! You guys rock!! thanks again.

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