English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Public Class Form1

Private Sub btnGuess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuess.Click
Dim guess As Integer
Dim x As Integer
Static intCount As Integer

intCount = intCount + 1

If intCount = 1 Then
lblNumber.Text = "You've guessed " & intCount & " time."
Else
lblNumber.Text = "You've guessed " & intCount & " times."
End If
guess = CInt(txtGuess.Text)
Randomize()
x = CInt(Int((100 * Rnd()) + 1))
If guess = x Then
' Generate random value between 1 and 100.
x = CInt(Int((100 * Rnd()) + 1))
intCount = 0
lblGuess.Text = "You geussed it!"
Else
If guess > x Then
lblGuess.Text = "Guess lower."
Else
If guess < x Then
lblGuess.Text = "Guess Higher."
End If

2007-03-08 13:25:10 · 1 answers · asked by Daddyhorhay 1 in Computers & Internet Programming & Design

Do I just use the onLoad function? Can I make the generator only happen when the form is opened? Feel free to put in the proper code and I can give it a try

2007-03-08 13:27:10 · update #1

Visual Basic. I want a random number at the opening of the program and then a new one only when i guess the correct number. Right now I am getting a new random every time I guess (click btnGuess after typing a number into txtGuess). You guys are awesome by the way on answering my previous questions.

2007-03-08 13:42:11 · update #2

1 answers

You can use a boolean variable to Lockout the random number generation after is has been done once. The boolean value can be set when loading the form and when a correct guess is made this will enable the random nmber to be generated. Immeadiately after the random number is generated the boolean is reset to lock out further updates until a correct guess is made.


declare module level variable

private m_AllowUpdate as boolean

in the load event initialize the variable m_AllowUpdate = true


in your guess button click event


if m_AllowUpdate then
place random number generation code here
disable further updates by setting m_AllowUpdate = false
end if


in the match checking rotine

if match then
reactivate random number generation m_AlloUpdate = true

2007-03-08 15:05:24 · answer #1 · answered by MarkG 7 · 0 0

fedest.com, questions and answers