473,654 Members | 3,071 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1692
Try this:

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

Dim bAlreadyBeenGue ssed As Boolean = False

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

Exit For
End If
Next

Then, if bAlreadyBeenGue ssed 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********@dis cussions.micros oft.com> wrote in message
news:<20******* *************** ************@mi crosoft.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********@dis cussions.micros oft.com> wrote in message
news:20******** *************** ***********@mic rosoft.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.Item s.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 "iGuessedNu m" is an integer in the text box (assuming you're
doing this in VB):

Dim bAlreadyBeenGue ssed As Boolean = False

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

Exit For
End If
Next

Then, if bAlreadyBeenGue ssed 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********@dis cussions.micros oft.com> wrote in message
news:<20******* *************** ************@mi crosoft.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********@dis cussions.micros oft.com> wrote in message
news:20******** *************** ***********@mic rosoft.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
"DoesNumberExis tInListBox":

----------------------------
Public Function DoesNumberExist InListBox(ByVal iGuessedNum As Integer)
As Boolean
Dim bExistsInListbo x As Boolean = False

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

Exit For
End If
Next

Return bExistsInListbo x
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 = DoesNumberExist InListBox(Me.Te xtBox1.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********@dis cussions.micros oft.com> wrote in message
news:0A******** *************** ***********@mic rosoft.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.Item s.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 "iGuessedNu m" is an integer in the text box (assuming you're
doing this in VB):

Dim bAlreadyBeenGue ssed As Boolean = False

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

Exit For
End If
Next

Then, if bAlreadyBeenGue ssed 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********@dis cussions.micros oft.com> wrote in message
news:<20******* *************** ************@mi crosoft.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********@dis cussions.micros oft.com> wrote in message
news:20******** *************** ***********@mic rosoft.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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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(txtGu ess.Text) Then
intGuess = CLng(txtGuess.T ext)
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 = DoesNumberExist InListBox(intGu ess)

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
"DoesNumberExis tInListBox":

----------------------------
Public Function DoesNumberExist InListBox(ByVal iGuessedNum As Integer)
As Boolean
Dim bExistsInListbo x As Boolean = False

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

Exit For
End If
Next

Return bExistsInListbo x
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 = DoesNumberExist InListBox(Me.Te xtBox1.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********@dis cussions.micros oft.com> wrote in message
news:0A******** *************** ***********@mic rosoft.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.Item s.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 "iGuessedNu m" is an integer in the text box (assuming you're
doing this in VB):

Dim bAlreadyBeenGue ssed As Boolean = False

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

Exit For
End If
Next

Then, if bAlreadyBeenGue ssed 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********@dis cussions.micros oft.com> wrote in message
news:<20******* *************** ************@mi crosoft.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********@dis cussions.micros oft.com> wrote in message
news:20******** *************** ***********@mic rosoft.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
4889
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 (MenusToHoldOpen == menuName) { MenusToHoldOpen.splice(i,1); return;
15
2527
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 row it stops.. At least that's what I thought I did, but VC71 says "warning C4717: 'LOOP<0,1>::DO' : recursive on all control paths, function will cause runtime stack overflow". What's wrong? Here's the code: template<int M, int N>
3
15954
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 loop. Is there some way to send a break from VBA code to break out of the loop? Here's what the error handler code looks like: Private Function MyFunction On Error GoTo HandleErr
15
2669
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 "foreach" loop instead of a "for" loop. This made absolutely no sense to me, but the author had posted his code and timing results. The "foreach" (a VB and other languages construct) was 0.01 seconds to access 1000 lines in rich text box,...
8
1685
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 elem.innerText = "1." Then elem.click() End If End If Next elemIndex
4
2220
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, either randomly or in a loop (I have a number of small, cool, computer-like sounds). I can get one sound to play for the keypress event, but don't know how to play multiple sounds. Here's the codes for playing one sound...
7
3193
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
1461
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 (field.type == 'text') { alert(field.name); // checkSearchInput(field);
52
6312
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 variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the distinct groups
2
1860
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 duplicate so many time, also i'm using a removeloop if the client want to remove the frame, in the html of the page i'm creating a table and i'm calling this loop, the problem now is that when i'm calling the removeloop it removes the frame but not the...
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.