Hey,just have a problem with some code,just starting out
Its basically a guessing game in vb

Public Class frmMain

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

        Dim randNum As Integer
        Dim guess As Integer = Convert.ToInt32(txtGuess.Text)


        Randomize()
        randNum = Int(20 * Rnd()) + 1


        Do Until randNum = guess


            If guess > randNum Then
                txtHint.Text = "Too High"
            ElseIf guess < randNum Then
                txtHint.Text = "Too Low"
            End If

        Loop
        txtHint.Text = "Correct guess"

Recommended Answers

All 7 Replies

Your not too far off the mark.
First, you are generating another random number every time you click your button. That makes it hard to win! Put the random number code in the load event of the form.
Next, you are going into and endless do loop - Once your loop begins, you cannot enter another guess. No need for a loop, just more to your If statement.
As you take this further, you will want to make sure the input is numeric.
Have fun!

Public Class Form1
    Dim randNum As Integer
    Dim guess As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Randomize()
        guess = Convert.ToInt32(txtGuess.Text)
        'Get Rid of the loop here
        If guess > randNum Then
            txtHint.Text = "Too High"
        ElseIf guess < randNum Then
            txtHint.Text = "Too Low"
        Else
            txtHint.Text = "Correct guess"
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put your random number generation here.
        randNum = Int(20 * Rnd()) + 1
    End Sub
End Class

Thanks for that ken,

buzzy

Changed the code,every time it generates 0 as the random number??

Public Class frmMain
    Dim random_number As Integer
    Dim guess As Integer
    

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

        guess = Convert.ToInt32(txtGuess.Text)

        If guess > random_number Then
            txtHint.Text = "too high"
        ElseIf guess < random_number Then
            txtHint.Text = "too low"
        End If

        If guess = random_number Then
            txtHint.Text = "correct"
        End If


    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim random_number As Integer
        Randomize()
        random_number = Int(20 * Rnd()) + 1

Buzzy,

You need to declare your random number variable ONLY at the class level. When you declare a variable within a sub routine (Private Sub...), it is only accessible to that sub. You declared this at both the class, and again in the sub, so it reset to zero when you left the form load event and clicked your button.

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        Dim random_number As Integer 'YOU ALREADY DECLARED THIS - DON'T DECLARE IT HERE OR IT WILL LOOSE IT'S VALUE WHEN YOU EXIT THIS PART OF THE CODE.       
Randomize()        
random_number = Int(20 * Rnd()) + 1

So, since you need to use this variable throughout your application, declare it at the class level (at the top), then assign it the random value as the form loads, then refer to it in the button click sub.

There are many important points to understand with variables. Try reading up on variable scope and life.

Good Luck!

Thanks it's working fine now,just have a another problem,i have a splash screen and login form but when i login the main form doesnt show,do i have to code the main form???

After the login, try:

Mainform.Show

Just got it,that should be it for now.......hopefully ,thanks for your help

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.