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
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.
Re: Looping Through Text Boxes, i.e. txtNumber1 - txtNumber20
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.