Hi

Here is my question.

My project is basically a addition test for 5 to 9 year old children. On my form I have a button which when clicked, it inputs a number from 1 to 10,( which is chosen by the user via an combo box drop down), into ten text boxes. I have built and array and used a, for loop for this and it works fine.

Dim array() As TextBox = {Txtbox_AddUser1, Txtbox_AddUser2, Txtbox_AddUser3, Txtbox_AddUser4, Txtbox_AddUser5, Txtbox_AddUser6, Txtbox_AddUser7, Txtbox_AddUser8, Txtbox_AddUser9, Txtbox_AddUser10}

        Dim i As Integer

        For i = 0 To 9
            array(i).Text = ComBox_ChooseNum.Text
        Next

My problem is I need to create another array connected to the same button to genarate 10 random numbers. Which will allow the user to add the two numbers together.
My problem is that visual basic wont let me add a second array to the same button?

How do I get round this problem

Thanks
steve

done it, i gave the two arrays diffrent names,,, silly me

1. Give your Array a different Variable Name for each Array declared.

Dim arMyCoolTextBoxesArray() As TextBox = {TextBox1, TextBox2, TextBox3}

        Dim arMyOtherCoolTextBoxesArray() As TextBox = {TextBox4, TextBox5, TextBox6}

2. Use the same Array and just change the values.

Dim arMyCoolTextBoxesArray() As TextBox = {TextBox1, TextBox2, TextBox3}
        For i As Integer = 0 To arMyCoolTextBoxesArray.Length - 1
            arMyCoolTextBoxesArray(i).Text = ComBox_ChooseNum.Text
        Next

        arMyCoolTextBoxesArray = {TextBox4, TextBox5, TextBox6}
        For i As Integer = 0 To arMyCoolTextBoxesArray.Length - 1
            arMyCoolTextBoxesArray(i).Text = ComBox_ChooseNum.Text
        Next

3. Skip the entire Array and set values in each TextBox once it is located by the # at the end of a TextBox's .Name.

'// my TextBoxes are named TextBox1, TextBox2, TextBox3, etc..
        For i As Integer = 1 To 10
            CType(Me.Controls("TextBox" & i), TextBox).Text = ComBox_ChooseNum.Text
        Next

cool thanks,

I am looking at ways to keep my forms code to a minim, The purpose of the program allows a user to select a number from 1-10 via a combo box and displays a random list of 10 addition sums for that number.

The form then displays so that the user can input all the answers to the relevant sum.

To this I have created a, for loop for the 10 text boxes that the user enters there the answer into. Like so.

Dim i As String
        i = +10


        If TxtBox_UserAns1.Text = Val(Txtbox_AddRan1.Text) + Val(Txtbox_AddUser1.Text) Then

            ans1 = Val(Txtbox_AddRan1.Text) + Val(Txtbox_AddUser1.Text)
            TxtBox_CorrectAns1.Text = ans1

            PitBox_mark1.Image = My.Resources.tick
            Lbl_TotalMark.Text = Val(Lbl_TotalMark.Text) + (i) & "%"
        Else

            ans1 = Val(Txtbox_AddRan1.Text) + Val(Txtbox_AddUser1.Text)
            TxtBox_CorrectAns1.Text = ans1

            PitBox_mark1.Image = My.Resources.cross
            Lbl_TotalMark.Text = Val(Lbl_TotalMark.Text) + (0) & "%"
        End If

And so on for each of the 9 other text box. The code is basically adding the chosen number from the combo box the user selected and adding it to the random generated number, then checking to see if it matches the answer that the user gave, if it does a image of a tick is shown or a cross if it’s wrong, it also prints out the correct answer to a label, and for each correct answer the code adds 10 to produce a percentage of correct answers.

Is there any other ways this can be done without all the bulky code, as I said .. I am looking at ways to keep my forms code to a minim

here is a screen shot of my form

For future references, please start a new thread for any "non related" questions.

Since you have already provided information, even a "cute" image:D that looks very nice by the way, see if this helps.

Public Class Form1
    Private iPercentage As Integer = 0 '// used to get percentage of correct answers.
    Private iCorrectAnswer As Integer = 0 '// used to add up random # and selected # to get the correct total.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        iPercentage = 0 '// reset.
        '// send appropriate controls to the Sub to get results.
        getCoolUserTotal(Txtbox_AddRan1, Txtbox_AddUser1, TxtBox_CorrectAns1, TxtBox_UserAns1, PitBox_mark1)
        getCoolUserTotal(Txtbox_AddRan2, Txtbox_AddUser2, TxtBox_CorrectAns2, TxtBox_UserAns2, PitBox_mark2)
        getCoolUserTotal(Txtbox_AddRan3, Txtbox_AddUser3, TxtBox_CorrectAns3, TxtBox_UserAns3, PitBox_mark3)
        '// etc..
        Lbl_TotalMark.Text = iPercentage & "%" '// display pecentage of correct answers.
    End Sub

    Private Sub getCoolUserTotal(ByVal txtForRandomNumber As TextBox, ByVal txtForSelectedNumber As TextBox, ByVal txtForCorrectAnswer As TextBox, ByVal txtForUserAnswer As TextBox, ByVal pbForImage As PictureBox)
        '// get correct total of random # and selected #.
        iCorrectAnswer = CInt(txtForRandomNumber.Text) + CInt(txtForSelectedNumber.Text) '// CInt = Convert to Integer since .Text is a String.
        '// check user total with the correct answer.
        If txtForUserAnswer.Text = CStr(iCorrectAnswer) Then '// if correct answer.
            pbForImage.Image = My.Resources.tick '// set image.
            iPercentage += 10 '// only add to percentage if correct answer.
        Else '// if wrong answer.
            pbForImage.Image = My.Resources.cross '// set image.
        End If
        '// display correct answer.
        txtForCorrectAnswer.Text = CStr(iCorrectAnswer) '// CStr = Convert to String since .Text is a String.
    End Sub
End Class

cool thanks for all your help. its working, sorry for the delay.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.