I am trying to create a game in VB6 where the user guess a number in a certain range depending on the level (Easy 1-5, Medium 1-15, Hard 1-30). I can't get the game to create a random number.

Private Sub cmdGo_Click()

[B]Dim Randomize As New Random[/B]

If optEasy.Value = True Then
    lblNumber.Visible = False
    lblWas.Visible = False
    lblWin.Visible = False
    [B]lblNumber.Caption = Randomize.Next(1, 5)[/B]
    If txtAnswer.Text = lblNumber.Text Then
        lblNumber.Visible = True
        lblWas.Visible = True
        lblWin.Visible = True
        lblWin.Caption = "YOU ARE CORRECT !!!"
        cmdGo.Enables = False
        txtAnswer.Enabled = False
    Else
        lblNumber.Visible = True
         lblWas.Visible = True
        lblWin.Visible = True
         lblWin.Caption = "You are WRONG. :( Sorry."
    End If

ok, let's imagine that you have a textbox and a command button, and when the user press the command button you create a random number, and then we compare the textbox with the variable (the random number). If they're the same, the user win. If they aren't, computer wins...

'First, we'll declare some variables.
dim myrandom,a as integer

public sub command1_click()
randomize
myrandom=int((10*rnd)+1)
a=cint(text1.text)
If a=myrandom then
Msgbox "You win!"
Else
Msgbox "Try it again."
End If
End Sub

If you want to, you can also add a boolean var to check if what the user writes in the textbox is a number, with IsNumeric. For example:

dim numornot as boolean
public sub command1_click()
numornot=isNumeric(text1.text)
if numornot=true then
'write here the code if the text is a number
Else
msgbox "you have to enter a number"
End If

If you need more help, I will be glad to answer your questions here or you can send me an e-mail: killbill07@hotmail.es.
Good luck.

Edited 6 Years Ago by killbill07: n/a

I want to generate the random number and insert it into a label. How can I do that ?

Insert the label and in the command1_click event write this:

label1.caption=a

Insert that code before the If.

This article has been dead for over six months. Start a new discussion instead.