Results 1 to 4 of 4

Thread: Looping Through Text Boxes, i.e. txtNumber1 - txtNumber20

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    6

    Looping Through Text Boxes, i.e. txtNumber1 - txtNumber20

    I'm in an introductory VB.net class and I have this assignment that has an answer key stored in an array. The assignment is to first draw the GUI, which have already done and I've attached it. Now, I was wondering if there is a way to loop through a bunch of text boxes, like txtNumber1 - txtNumber20. Something like a For Next loop where I could put in x, i.e. txtNumberx and go from 1-20. My instructor is ok, but he is not the best. From what I understand, you could do something like this in VB 6, but not in VB.net? Is this true? Or do I have to type a bunch of lines of code like the following? Thanks.

    Code:
    Public Class frmDriversLicenseExam
    
        Dim strAnswerKey() As String = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"}
    
        Dim strStudentAnswers(20) As String
    
        Dim strNumberOne As String = ""
        Dim strNumberTwo As String = ""
        Dim strNumberThree As String = ""
        Dim strNumberFour As String = ""
        Dim strNumberFive As String = ""
        Dim strNumberSix As String = ""
        Dim strNumberSeven As String = ""
        Dim strNumberEight As String = ""
        Dim strNumberNine As String = ""
        Dim strNumberTen As String = ""
        Dim strNumberEleven As String = ""
        Dim strNumberTwelve As String = ""
        Dim strNumberThirteen As String = ""
        Dim strNumberFourteen As String = ""
        Dim strNumberFifteen As String = ""
        Dim strNumberSixteen As String = ""
        Dim strNumberSeventeen As String = ""
        Dim strNumberEighteen As String = ""
        Dim strNumberNineteen As String = ""
        Dim strNumberTwenty As String = ""
    
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Me.Close()
        End Sub
    
        Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
    
        End Sub
    
        Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    
        End Sub
    
        Private Sub btnScoreExam_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScoreExam.Click
            strNumberOne = txtNumber1.Text
            strNumberTwo = txtNumber2.Text
            strNumberThree = txtNumber3.Text
            strNumberFour = txtNumber4.Text
            strNumberFive = txtNumber5.Text
            strNumberSix = txtNumber6.Text
            strNumberSeven = txtNumber7.Text
            strNumberEight = txtNumber8.Text
            strNumberNine = txtNumber9.Text
            strNumberTen = txtNumber10.Text
            strNumberEleven = txtNumber11.Text
            strNumberTwelve = txtNumber12.Text
            strNumberThirteen = txtNumber13.Text
            strNumberFourteen = txtNumber14.Text
            strNumberFifteen = txtNumber15.Text
            strNumberSixteen = txtNumber16.Text
            strNumberSeventeen = txtNumber17.Text
            strNumberEighteen = txtNumber18.Text
            strNumberNineteen = txtNumber19.Text
            strNumberTwenty = txtNumber20.Text
        End Sub
    
        Sub InputIntoArray()
            strStudentAnswers(0) = strNumberOne
            strStudentAnswers(1) = strNumberTwo
            strStudentAnswers(2) = strNumberThree
            strStudentAnswers(3) = strNumberFour
            strStudentAnswers(4) = strNumberFive
            strStudentAnswers(5) = strNumberOne
            strStudentAnswers(6) = strNumberOne
            strStudentAnswers(7) = strNumberOne
            strStudentAnswers(8) = strNumberOne
            strStudentAnswers(9) = strNumberOne
            strStudentAnswers(10) = strNumberOne
            strStudentAnswers(11) = strNumberOne
            strStudentAnswers(12) = strNumberOne
            strStudentAnswers(13) = strNumberOne
            strStudentAnswers(14) = strNumberOne
            strStudentAnswers(15) = strNumberOne
            strStudentAnswers(16) = strNumberOne
            strStudentAnswers(17) = strNumberOne
            strStudentAnswers(18) = strNumberOne
            strStudentAnswers(19) = strNumberOne
        End Sub
    End Class
    Attached Images Attached Images  

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Looping Through Text Boxes, i.e. txtNumber1 - txtNumber20

    well there are no control arrays in VB.NET as there was in VB6, however you can simply just create an array of textboxes and assign your forms textboxes to that array, which you can loop through.

    The other option is to loop the array of controls on the form using me.controls, however this will loop ALL controls on the form, and they arent in the order they appear on the form.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    6

    Re: Looping Through Text Boxes, i.e. txtNumber1 - txtNumber20

    Quote Originally Posted by kleinma
    well there are no control arrays in VB.NET as there was in VB6, however you can simply just create an array of textboxes and assign your forms textboxes to that array, which you can loop through.

    The other option is to loop the array of controls on the form using me.controls, however this will loop ALL controls on the form, and they arent in the order they appear on the form.

    Can you please give me some example code?

    Thanks

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Looping Through Text Boxes, i.e. txtNumber1 - txtNumber20

    here is a small example app with 8 textboxes. clicking the button will loop them all and show a messagebox with their contents...

    this example should give you all you need.
    Attached Files Attached Files

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