Hi guys,

See below. I actually broke this when trying to fix it (trying to add in the return 1,2,3 for the different possibilities), but basically I'm currently struggling with using forms. Using 2 random numbers the user must enter the correct answer for multiplying the random numbers. How can I empty the fields and basically reset the form after a correct answer ? And what is essentially wrong with what i have done ?

Public Class MathLoader

    Dim generator As New Random

    Dim a1 As Integer = generator.Next(1, 100)
    Dim b1 As Integer = generator.Next(1, 100)


    Public MathLoader()


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub


    Private Function correctCheck() As Integer

        Dim z1 As Double = a1 * b1
        Dim x1 As Integer = 0

        If TextBox1.Text = "" Then
            Label3.Text = "Please enter a value"
            Return 2

        ElseIf TextBox1.Text = z1 Then     

            Label3.Text = "Correct !"
            ans1.Text = z1
            Beep()
            Return 1

        Else
            Label3.Text = "Wrong..."
            Return 3

        End If

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Try

        ran1.Text = a1
        ran2.Text = b1

        If ans1.Text = "" Then
            MessageBox.Show("Please enter a response")

        ElseIf ans1.Text <> "" Then
            For index = 1 To 3
                If Me.correctCheck() = 1 Then
                    MessageBox.Show("Complete")
                ElseIf Me.correctCheck() = 2 Then
                    MessageBox.Show("2")
                ElseIf Me.correctCheck() = 3 Then
                    MessageBox.Show("3")
                Else
                    MessageBox.Show("4")
                End If
            Next
            MessageBox.Show("You're out of guesses")

        End If

        'Catch e As Exception
        '    MessageBox.Show(e.ToString)

        'End Try

   End Sub

End Class

I don't know what the difference is between ans1 and TextBox1 so i just ignored that.
Here is a complete code for your guessing form:

Public Class Form1

	Dim guess As Integer


	Private Sub StartGame()
		Dim generator As New Random
		ran1.Text = CStr(generator.Next(1, 100))
		ran2.Text = CStr(generator.Next(1, 100))
		ans1.Text = String.Empty
		Label1.Text = String.Empty
		guess = 1
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                Label1.Text = String.Empty
		Dim result As Integer = CInt(ran1.Text) * CInt(ran2.Text)
		Dim userInput As Integer
		If Not Integer.TryParse(ans1.Text, userInput) Then
			MsgBox("Please enter an Integer.")
			Return
		End If


		If result = userInput Then
			MsgBox("Well done, result is correct!")
			StartGame()
			Return
		End If

		If guess = 4 Then
			MsgBox(String.Format("You're out of guesses.{0}The correct answer was: {1}", Environment.NewLine, result))
			StartGame()
			Return
		End If

		Label1.Text = String.Format("Wrong! Guess: {0}", guess)
		guess += 1

	End Sub

	Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
		Randomize()	'avoid to start with the same random numbers on every application startup
		StartGame()
	End Sub
End Class
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.