473,321 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

For Loop function?

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
4 1670
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: reynoldscraigr | last post by:
Hi All, hope someone can see what wrong here I have the following function function RemoveMenuFromHoldArray(menuName) { var i = 0; for (i=0;i<=MenusToHoldOpen.length-1;i++) { if...
15
by: Robin Eidissen | last post by:
What I try to do is to iterate over two variables using template metaprogramming. I've specialized it such that when it reaches the end of a row ot starts on the next and when it reaches the last...
3
by: deko | last post by:
I have a logging routine that's supposed to silently log errors caught by error handler code on certain functions. The problem is sometimes stuff happens and the error handler can get caught in a...
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
8
by: Microsoft | last post by:
I have the following loop the length contains somewhere around 1000+ items: For elemIndex = 0 To len - 1 elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement) If elem.tagName = "A" Then If...
4
by: Arnold | last post by:
Hi there, Here's the situation--there is a text field in a form in which students will key in data. On the keypress event, I'd like for different sounds to be played for each character typed,...
7
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
12
by: usa-99 | last post by:
Hi there I have following function which is called on load of page. function checkFieldContent(form) { var field; for(i = 0; i < form.elements.length; i++) { field = form.elements; if...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
2
by: mrjoka | last post by:
hi experts, i'm developing a page in ASP but i'm doing also some javascript insode the page. i'm creating a frame and i want to loop this frame with a duplicateloop function so the form will be...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.