Newbie - Listbox Validation??

G

Guest

Hi,

I'm learning Visual basic.net and I am new to the world of developing and
coding. I am having a practice creating a very simple game.

Baiscally, there is a textbox, button and listbox.

What I am doing is a Guess the number game.

I have created the form and everything on it and it all works fine. The
program generates a random number, and the user has to enter a number into
the textbox and click the button to see how close they are to the number or
if they have got it correct, by way of a message box popping up saying
whether its too low, high or spot on. Also, the numers entered are copied to
a listbox.

What I want to do is...

Put some code in to validate what numbers have already been entered, so if
someone enteres 5 into the textbox and its wrong, 5 gets inputted into the
listbox, if they enter 5 again, I want something to say, "youve already tried
this number - try again"

Ive trawled everywhere and come accross Loops which should do this, but I
cant get my head around it... Any guidence.

Please remember I'm new and am learning by doing basic things like this! to
learn what the functions do etc...

Thanks

PS... Code follows.....


Public Class Form1
Inherits System.Windows.Forms.Form
Private intNumberToGuess As Long

Private Sub Button1_Enter(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGuess.Click
Dim intGuess As Long
Dim UpperBound As Long
Dim LowerBound As Long

'check the number being entered into the textbox against the random
number set

If IsNumeric(txtGuess.Text) Then
intGuess = CLng(txtGuess.Text)
Else

' If no text enetered and button clciked, display message

MsgBox("Enter a number into the textbox!")
End If

' If the number being enetered is lower than the random number,
update

If intGuess < intNumberToGuess Then
lblInfo.Text() = "Too Low!"
MsgBox("Number is Too Low - Have another go!")

ElseIf intGuess > intNumberToGuess Then
lblInfo.Text = "Too High!"
MsgBox("Number is Too High - Have another go!")

ElseIf intGuess = intNumberToGuess Then
lblInfo.Text = "You've Got It!"
MsgBox("Well Done - You've worked out the random number - Now
have another go!")

intNumberToGuess = Int((UpperBound - LowerBound + 100) * Rnd() +
LowerBound)

End If

'Add the numbers guessed into the listbox

listGuess.Items.Add(txtGuess.Text)

'check if the number in the textbox is in the listbox


End Sub


Private Sub listGuess_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
listGuess.SelectedIndexChanged

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim intNumberToGuess As Long
Dim UpperBound As Long
Dim LowerBound As Long

Randomize()

intNumberToGuess = Int((UpperBound - LowerBound + 100) * Rnd() +
LowerBound)
End Sub
 
C

Ciaran

My suggestiong would be to add the numbers guessed to a hashtable as
integers. Then when a guess occurs check to see if they are in the hashtable
or not.

Replace you Button click with this:

Private previousGuesses As New System.Collections.Hashtable

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

Dim intGuess As Long

Dim UpperBound As Long

Dim LowerBound As Long

'check the number being entered into the textbox against the random number
set

If IsNumeric(txtGuess.Text) Then

intGuess = CLng(txtGuess.Text)

Else

' If no text enetered and button clciked, display message

MsgBox("Enter a number into the textbox!")

End If

' If the number being enetered is lower than the random number,

Update()

If Not (previousGuesses(intGuess) Is Nothing) Then

lblInfo.Text() = "youve already tried this number - try again"

MsgBox("youve already tried this number - try again")

Else

If intGuess < intNumberToGuess Then

lblInfo.Text() = "Too Low!"

MsgBox("Number is Too Low - Have another go!")

ElseIf intGuess > intNumberToGuess Then

lblInfo.Text = "Too High!"

MsgBox("Number is Too High - Have another go!")

ElseIf intGuess = intNumberToGuess Then

lblInfo.Text = "You've Got It!"

MsgBox("Well Done - You've worked out the random number - Now have another
go!")

intNumberToGuess = Int((UpperBound - LowerBound + 100) * Rnd() + LowerBound)

End If

'Add the numbers guessed into the listbox

listGuess.Items.Add(txtGuess.Text)

previousGuesses(intGuess) = intGuess

End If



End Sub





Ciaran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top