By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

For Loop function?

P: n/a
Hi,

I'm new to this so I could really do with some help!

I am using VB.net to create a small game. basically there is a Textbox, a
button and a listbox.

The user enters a number into the textbox which is then validated against a
random number which is generated using the Rnd function. When the user clicks
the button, they either get a message saying Too Low, Too High or Spot On.
This number is then copied into the listbox as a reminder of whats been
guessed.

What I need is a function to run which checks whats already been entered and
displays a message saying that its "already been tried" if its in the listbox.

Basically, HOW do i do this? I have tried trawling the net and newsgroups
and books but cant work it out.

Can anyone help?
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Try this:

Given that "iGuessedNum" is an integer in the text box (assuming you're
doing this in VB):

Dim bAlreadyBeenGuessed As Boolean = False

For Each strItem As String In ListBox1.Items
If strItem = iGuessedNum Then
bAlreadyBeenGuessed = True

Exit For
End If
Next

Then, if bAlreadyBeenGuessed is true, then the number has already been
guessed.

This is a bit of a sloppy example doing implicit conversions but you get
the point of how to enumerate through items in a listbox.

Brandon

"Joneseyboy" <Jo********@discussions.microsoft.com> wrote in message
news:<20**********************************@microso ft.com>...
Hi,

I'm new to this so I could really do with some help!

I am using VB.net to create a small game. basically there is a Textbox, a button and a listbox.

The user enters a number into the textbox which is then validated against a random number which is generated using the Rnd function. When the user clicks the button, they either get a message saying Too Low, Too High or Spot On. This number is then copied into the listbox as a reminder of whats been guessed.

What I need is a function to run which checks whats already been entered and displays a message saying that its "already been tried" if its in the listbox.
Basically, HOW do i do this? I have tried trawling the net and newsgroups and books but cant work it out.

Can anyone help?

"Joneseyboy" <Jo********@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com... Hi,

I'm new to this so I could really do with some help!

I am using VB.net to create a small game. basically there is a Textbox, a
button and a listbox.

The user enters a number into the textbox which is then validated against a random number which is generated using the Rnd function. When the user clicks the button, they either get a message saying Too Low, Too High or Spot On.
This number is then copied into the listbox as a reminder of whats been
guessed.

What I need is a function to run which checks whats already been entered and displays a message saying that its "already been tried" if its in the listbox.
Basically, HOW do i do this? I have tried trawling the net and newsgroups
and books but cant work it out.

Can anyone help?

Jul 21 '05 #2

P: n/a
Brandon,

You're a star - Thank you!

I know that this is is really simple for some people and looking at what the
actual functions are Iv'e sort of understood it, like renaming the items to
be the same as mine etc.. Ive also added a message box in so that if the
number is entered more than once it says so

What I also need to do, is prevent the number now being added to the listbox
again... So how can i prevent the rest of the code running which does the
adding of the number in the Add the numbers guessed into the listbox? ie
(listGuess.Items.Add(txtGuess.Text)) and displays messages and checks whether
the number being guessed is <=> the random number?

Sorry to be a pain!


"Brandon Potter" wrote:
Try this:

Given that "iGuessedNum" is an integer in the text box (assuming you're
doing this in VB):

Dim bAlreadyBeenGuessed As Boolean = False

For Each strItem As String In ListBox1.Items
If strItem = iGuessedNum Then
bAlreadyBeenGuessed = True

Exit For
End If
Next

Then, if bAlreadyBeenGuessed is true, then the number has already been
guessed.

This is a bit of a sloppy example doing implicit conversions but you get
the point of how to enumerate through items in a listbox.

Brandon

"Joneseyboy" <Jo********@discussions.microsoft.com> wrote in message
news:<20**********************************@microso ft.com>...
Hi,

I'm new to this so I could really do with some help!

I am using VB.net to create a small game. basically there is a

Textbox, a
button and a listbox.

The user enters a number into the textbox which is then validated

against a
random number which is generated using the Rnd function. When the user

clicks
the button, they either get a message saying Too Low, Too High or Spot

On.
This number is then copied into the listbox as a reminder of whats

been
guessed.

What I need is a function to run which checks whats already been

entered and
displays a message saying that its "already been tried" if its in the

listbox.

Basically, HOW do i do this? I have tried trawling the net and

newsgroups
and books but cant work it out.

Can anyone help?

"Joneseyboy" <Jo********@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
Hi,

I'm new to this so I could really do with some help!

I am using VB.net to create a small game. basically there is a Textbox, a
button and a listbox.

The user enters a number into the textbox which is then validated against

a
random number which is generated using the Rnd function. When the user

clicks
the button, they either get a message saying Too Low, Too High or Spot On.
This number is then copied into the listbox as a reminder of whats been
guessed.

What I need is a function to run which checks whats already been entered

and
displays a message saying that its "already been tried" if its in the

listbox.

Basically, HOW do i do this? I have tried trawling the net and newsgroups
and books but cant work it out.

Can anyone help?


Jul 21 '05 #3

P: n/a
If I understand correctly you don't want to have a number x (let's say 3,
for instance) added to the listbox twice.

From a design perspective here, you have 2 decisions the program needs to
make that both require one question to be answered: "Does this number
already exist in the listbox?" Therefore, this calls for a function. For
example, I've taken the code below and made a function called
"DoesNumberExistInListBox":

----------------------------
Public Function DoesNumberExistInListBox(ByVal iGuessedNum As Integer)
As Boolean
Dim bExistsInListbox As Boolean = False

For Each strItem As String In ListBox1.Items
If strItem = iGuessedNum Then
bExistsInListbox = True

Exit For
End If
Next

Return bExistsInListbox
End Function
----------------------------

Now, you can find out if the number has been guessed, and not insert it into
the listbox like so:

Dim bAlreadyGuessed As Boolean

bAlreadyGuessed = DoesNumberExistInListBox(Me.TextBox1.Text)

If bAlreadyGuessed Then
MessageBox.Show("You've already guessed that number!")
Else
ListBox1.Items.Add(Me.TextBox1.Text)
End If

I'm not completely sure if that's what you're looking for, but let us know!

Brandon

"Joneseyboy" <Jo********@discussions.microsoft.com> wrote in message
news:0A**********************************@microsof t.com...
Brandon,

You're a star - Thank you!

I know that this is is really simple for some people and looking at what the actual functions are Iv'e sort of understood it, like renaming the items to be the same as mine etc.. Ive also added a message box in so that if the
number is entered more than once it says so

What I also need to do, is prevent the number now being added to the listbox again... So how can i prevent the rest of the code running which does the
adding of the number in the Add the numbers guessed into the listbox? ie
(listGuess.Items.Add(txtGuess.Text)) and displays messages and checks whether the number being guessed is <=> the random number?

Sorry to be a pain!


"Brandon Potter" wrote:
Try this:

Given that "iGuessedNum" is an integer in the text box (assuming you're
doing this in VB):

Dim bAlreadyBeenGuessed As Boolean = False

For Each strItem As String In ListBox1.Items
If strItem = iGuessedNum Then
bAlreadyBeenGuessed = True

Exit For
End If
Next

Then, if bAlreadyBeenGuessed is true, then the number has already been
guessed.

This is a bit of a sloppy example doing implicit conversions but you get
the point of how to enumerate through items in a listbox.

Brandon

"Joneseyboy" <Jo********@discussions.microsoft.com> wrote in message
news:<20**********************************@microso ft.com>...
Hi,

I'm new to this so I could really do with some help!

I am using VB.net to create a small game. basically there is a

Textbox, a
button and a listbox.

The user enters a number into the textbox which is then validated

against a
random number which is generated using the Rnd function. When the user

clicks
the button, they either get a message saying Too Low, Too High or Spot

On.
This number is then copied into the listbox as a reminder of whats

been
guessed.

What I need is a function to run which checks whats already been

entered and
displays a message saying that its "already been tried" if its in the

listbox.

Basically, HOW do i do this? I have tried trawling the net and

newsgroups
and books but cant work it out.

Can anyone help?

"Joneseyboy" <Jo********@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
Hi,

I'm new to this so I could really do with some help!

I am using VB.net to create a small game. basically there is a Textbox, a button and a listbox.

The user enters a number into the textbox which is then validated against
a
random number which is generated using the Rnd function. When the user

clicks
the button, they either get a message saying Too Low, Too High or Spot
On. This number is then copied into the listbox as a reminder of whats been guessed.

What I need is a function to run which checks whats already been entered and
displays a message saying that its "already been tried" if its in the

listbox.

Basically, HOW do i do this? I have tried trawling the net and

newsgroups and books but cant work it out.

Can anyone help?


Jul 21 '05 #4

P: n/a
Thanks Brandon,

This is the code I have now after amending it to fit my control names:

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
'check if the number in the textbox is in the listbox and display a
message if it is
Dim bAlreadyGuessed As Boolean

bAlreadyGuessed = DoesNumberExistInListBox(intGuess)

If bAlreadyGuessed Then
MessageBox.Show("You've already guessed that number!")
Else
listGuess.Items.Add(intGuess)
' And it now works like a treat!

Thanks for your help!

"Brandon Potter" wrote:
If I understand correctly you don't want to have a number x (let's say 3,
for instance) added to the listbox twice.

From a design perspective here, you have 2 decisions the program needs to
make that both require one question to be answered: "Does this number
already exist in the listbox?" Therefore, this calls for a function. For
example, I've taken the code below and made a function called
"DoesNumberExistInListBox":

----------------------------
Public Function DoesNumberExistInListBox(ByVal iGuessedNum As Integer)
As Boolean
Dim bExistsInListbox As Boolean = False

For Each strItem As String In ListBox1.Items
If strItem = iGuessedNum Then
bExistsInListbox = True

Exit For
End If
Next

Return bExistsInListbox
End Function
----------------------------

Now, you can find out if the number has been guessed, and not insert it into
the listbox like so:

Dim bAlreadyGuessed As Boolean

bAlreadyGuessed = DoesNumberExistInListBox(Me.TextBox1.Text)

If bAlreadyGuessed Then
MessageBox.Show("You've already guessed that number!")
Else
ListBox1.Items.Add(Me.TextBox1.Text)
End If

I'm not completely sure if that's what you're looking for, but let us know!

Brandon

"Joneseyboy" <Jo********@discussions.microsoft.com> wrote in message
news:0A**********************************@microsof t.com...
Brandon,

You're a star - Thank you!

I know that this is is really simple for some people and looking at what

the
actual functions are Iv'e sort of understood it, like renaming the items

to
be the same as mine etc.. Ive also added a message box in so that if the
number is entered more than once it says so

What I also need to do, is prevent the number now being added to the

listbox
again... So how can i prevent the rest of the code running which does the
adding of the number in the Add the numbers guessed into the listbox? ie
(listGuess.Items.Add(txtGuess.Text)) and displays messages and checks

whether
the number being guessed is <=> the random number?

Sorry to be a pain!


"Brandon Potter" wrote:
Try this:

Given that "iGuessedNum" is an integer in the text box (assuming you're
doing this in VB):

Dim bAlreadyBeenGuessed As Boolean = False

For Each strItem As String In ListBox1.Items
If strItem = iGuessedNum Then
bAlreadyBeenGuessed = True

Exit For
End If
Next

Then, if bAlreadyBeenGuessed is true, then the number has already been
guessed.

This is a bit of a sloppy example doing implicit conversions but you get
the point of how to enumerate through items in a listbox.

Brandon

"Joneseyboy" <Jo********@discussions.microsoft.com> wrote in message
news:<20**********************************@microso ft.com>...
> Hi,
>
> I'm new to this so I could really do with some help!
>
> I am using VB.net to create a small game. basically there is a
Textbox, a
> button and a listbox.
>
> The user enters a number into the textbox which is then validated
against a
> random number which is generated using the Rnd function. When the user
clicks
> the button, they either get a message saying Too Low, Too High or Spot
On.
> This number is then copied into the listbox as a reminder of whats
been
> guessed.
>
> What I need is a function to run which checks whats already been
entered and
> displays a message saying that its "already been tried" if its in the
listbox.
>
> Basically, HOW do i do this? I have tried trawling the net and
newsgroups
> and books but cant work it out.
>
> Can anyone help?
"Joneseyboy" <Jo********@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
> Hi,
>
> I'm new to this so I could really do with some help!
>
> I am using VB.net to create a small game. basically there is a Textbox, a > button and a listbox.
>
> The user enters a number into the textbox which is then validated against a
> random number which is generated using the Rnd function. When the user
clicks
> the button, they either get a message saying Too Low, Too High or Spot On. > This number is then copied into the listbox as a reminder of whats been > guessed.
>
> What I need is a function to run which checks whats already been entered and
> displays a message saying that its "already been tried" if its in the
listbox.
>
> Basically, HOW do i do this? I have tried trawling the net and newsgroups > and books but cant work it out.
>
> Can anyone help?


Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.