 |
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

May 22nd, 2007, 04:55 AM
|
Registered User
|
|
Join Date: May 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Lottery Program Help...I'm using 2005 Visual Basic
Hello, i'm a new member to this thing. I worked long in working with ti 83 calculator BASIC language, but this visual basic 2005 is SOOOOOO different from it... I need help with writing a code for a lottery game. It is a school project, but I can't get the code to work. I got it so it would randomly generate a number from 1- 45, and you can type in numbers, than the part where you type in your guess numbers would be disabled and locked. Well, I can't make the part where it would make a MsgBox when all or partial numbers are correct. Please help. This is what I got so far.
Well, just to tell you, command1 (or here, button1) is the Check your number button, Text1~6 (or here, textbox 1~6) is the guess numbers, and Textbox 7~12 is the real lottery numbers.
Variable A, B, C, D, F, G (says that variable E was tooken somewhere) are used. The Variables are not required, but I thought when I made the code for the "Check your number button" I thought it would save some ram.
Label4 is the label that shows you the money that you have.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim A
A = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
Dim B
B = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
If B = A Then
B = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
End If
Dim C
C = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
If C = B Or A Then
C = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
End If
Dim D
D = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
If D = A Or B Or C Then
D = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
End If
Dim F
F = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
If F = A Or B Or C Or D Then
F = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
End If
Dim G
G = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
If G = A Or B Or C Or D Or F Then
G = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
End If
TextBox12.Text = A
TextBox11.Text = B
TextBox10.Text = C
TextBox9.Text = D
TextBox8.Text = F
TextBox7.Text = G
End Sub
This is the code to make the random number when the form will load.
I don't know if this is correct... I don't know but I think I got it so it would not create the same number for 2 or more text boxes.
If TextBox2.Text > 45 Then
MsgBox("0~45", MsgBoxStyle.Exclamation, "Change number")
TextBox2.Text = 0
End If
This is the code for when you type in a number (it is for the text.change event) too high then it would make you to change the number. This works correctly, and if you have any suggestions to make it compressed, please tell me. I put this code for every textboxes where you type in your guess.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label4.Text = Val(Label4.Text) - 10
If Label4.Text <= 0 Then
If MsgBox("Bankrupt!", MsgBoxStyle.OkOnly, "You are out of Money!") = MsgBoxResult.Ok Then
End
End If
End If
TextBox7.Visible = True
TextBox8.Visible = True
TextBox9.Visible = True
TextBox10.Visible = True
TextBox11.Visible = True
TextBox12.Visible = True
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox3.Enabled = False
TextBox4.Enabled = False
TextBox5.Enabled = False
TextBox6.Enabled = False
Dim A
A = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
Dim B
B = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
If B = A Then
B = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
End If
Dim C
C = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
If C = B Or A Then
C = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
End If
Dim D
D = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
If D = A Or B Or C Then
D = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
End If
Dim F
F = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
If F = A Or B Or C Or D Then
F = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
End If
Dim G
G = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
If G = A Or B Or C Or D Or F Then
G = Int((45 - 1 + 1) * Rnd() + 1)
Randomize()
End If
TextBox12.Text = A
TextBox11.Text = B
TextBox10.Text = C
TextBox9.Text = D
TextBox8.Text = F
TextBox7.Text = G
If Val(TextBox1.Text) = Val(TextBox12.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox1.Text) = Val(TextBox11.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox1.Text) = Val(TextBox10.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox1.Text) = Val(TextBox9.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox1.Text) = Val(TextBox8.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox1.Text) = Val(TextBox7.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox2.Text) = Val(TextBox12.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox2.Text) = Val(TextBox11.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox2.Text) = Val(TextBox10.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox2.Text) = Val(TextBox9.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox2.Text) = Val(TextBox8.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox2.Text) = Val(TextBox7.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox3.Text) = Val(TextBox12.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox3.Text) = Val(TextBox11.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox3.Text) = Val(TextBox10.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox3.Text) = Val(TextBox9.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox3.Text) = Val(TextBox8.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox3.Text) = Val(TextBox7.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox4.Text) = Val(TextBox12.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox4.Text) = Val(TextBox11.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox4.Text) = Val(TextBox10.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox4.Text) = Val(TextBox9.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox4.Text) = Val(TextBox8.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox4.Text) = Val(TextBox7.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
If Val(TextBox1.Text) = A Or B Or C Or D Or F Or G Then
If Val(TextBox2.Text) = A Or B Or C Or D Or F Or G Then
If Val(TextBox3.Text) = A Or B Or C Or D Or F Or G Then
If Val(TextBox4.Text) = A Or B Or C Or D Or F Or G Then
If Val(TextBox5.Text) = A Or B Or C Or D Or F Or G Then
If Val(TextBox6.Text) = A Or B Or C Or D Or F Or G Then
MsgBox("You win a billion dollars!", MsgBoxStyle.Exclamation, "You win!")
Label4.Text = Val(Label4.Text) + 1000000000
MsgBox("10% tax rate off", MsgBoxStyle.Information, "10% off")
Label4.Text = Val(Label4.Text) - (Val(Label4.Text) * 0.1)
MsgBox("10% to the beloved charity", MsgBoxStyle.Information, "10% off")
Label4.Text = Val(Label4.Text) - (Val(Label4.Text) * 0.1)
MsgBox("20% to pay credit card and insurance bills", MsgBoxStyle.Information, "10% off")
Label4.Text = Val(Label4.Text) - (Val(Label4.Text) * 0.1)
End If
End If
End If
End If
End If
End If
End Sub
This is the part I am stuck on. The very first few lines for this code, is the part when your money is < 0 (less than 0) than a message box tells you that you are bankrupt and the program would end.
This part works.
The next part is when the textboxes 7~12 appear (the real lottery numbers) and the textboxes 1~6 are disabled so you can't change the numbers.
This part works, too.
The next long part is where if you get 1 number correct, than it would give you 10 more money each number. The last part is the multiple number part.
If Val(TextBox1.Text) = A Or B Or C Or D Or F Or G Then
If Val(TextBox2.Text) = A Or B Or C Or D Or F Or G Then
If Val(TextBox3.Text) = A Or B Or C Or D Or F Or G Then
If Val(TextBox4.Text) = A Or B Or C Or D Or F Or G Then
If Val(TextBox5.Text) = A Or B Or C Or D Or F Or G Then
If Val(TextBox6.Text) = A Or B Or C Or D Or F Or G Then
MsgBox("You win a billion dollars!", MsgBoxStyle.Exclamation, "You win!")
Label4.Text = Val(Label4.Text) + 1000000000
MsgBox("10% tax rate off", MsgBoxStyle.Information, "10% off")
Label4.Text = Val(Label4.Text) - (Val(Label4.Text) * 0.1)
MsgBox("10% to the beloved charity", MsgBoxStyle.Information, "10% off")
Label4.Text = Val(Label4.Text) - (Val(Label4.Text) * 0.1)
MsgBox("20% to pay credit card and insurance bills", MsgBoxStyle.Information, "10% off")
Label4.Text = Val(Label4.Text) - (Val(Label4.Text) * 0.1)
End If
End If
End If
End If
End If
End If
End Sub
This part. It's the only part that doesn't work. I don't get it, how to make it so you would get more money when multiple numbers are correct. Can anybody help???
|

May 22nd, 2007, 07:40 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
hi there...
first of all, using randomize every time doesn't make the number more "random", just randomize before the first call to rnd...
second:
Code:
Int((45 [u]- 1 + 1</u>) * Rnd() + 1)
the underscore part doesn't make any sense... are you sure you want to add 1 and then substract 1????
third:
Code:
If Val(TextBox1.Text) = Val(TextBox12.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
End If
you start with this, and do this to try textbox1, 2, 3 and 4 but never test 5 or 6 (maybe an error??)
ok.. here's what i see can be your problem, you can pay prices for all the numbers or one particular number (because you already got that with the if...endif that I point out before) but you can't pay prices for every other combination. so.. I have a better idea (since is homework i will tell you how you can do it, not the actual code)...
Write a function that return if a particular textbox has a match or not (you have to pass the textbox as a parameter) and make it return true if it has a match or false if not, Count the number of match.. and also you can put something on the form beside the textbox to mark it (if it a match) for the user....
feel free to continue asking :)
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|

May 22nd, 2007, 08:20 AM
|
Registered User
|
|
Join Date: May 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I got it so it would make a checkmark whenevery there is a match, by adding a line to each stuff...
If Val(TextBox1.Text) = Val(TextBox12.Text) Then
MsgBox("win $!!!", MsgBoxStyle.OkOnly, "Win money!")
Label4.Text = Val(Label4.Text) + 10
[u]Image6.Visible = True</u>
End If
But I still don't get how you do the combination of numbers. Do I have to use the word "or" in the code??
Like whenever I make something similar, then whenever I click my command (button) it would always say that I win a bill dollars ...
|

May 22nd, 2007, 08:22 AM
|
Registered User
|
|
Join Date: May 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Oh, and also the (45 - 1 + 1) part, I just took the - 1 + 1 out.
|

May 22nd, 2007, 08:31 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
nop.. you already do what i told you to put in a function... my idea was just to make it clear and allow you to count the number of correct numbers you have...
I will try with an example:
you have 6 numbers to match with 6 others..
you pass to this function the first number..
if compare it with every result (the method doesn't mater, but use six if...endif for clarify), after the first true case, exit the function with a true (return true), if you reach the end exit with false (return false)...
count every true you receive from this function and you have the number of correct answers...
better now???
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|

May 22nd, 2007, 08:33 AM
|
Registered User
|
|
Join Date: May 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
http://files-upload.com/240907/PrgmLottery.zip.html
here, it's my current thing without the image whenever there is a match, because if I want that to happen, I need to add in the images in the zip file, too, and I was a little lazy. Well anyways the image is an image of a checkmark. This file is like a pre- version. You might want to test it out- it's a lot easier if you know how my thing looks like.
|

May 22nd, 2007, 08:39 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
please, first try the solution that I'm trying to give to you..
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|

May 22nd, 2007, 08:42 AM
|
Registered User
|
|
Join Date: May 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Oh! I got it! For every matches, there will be a "Label5.text = Val(Label5.text) + 1" event, and an If Else statement where
If Val(Label5.text) = 1 Then
MsgBox about adding 10 to label 4
End If
etc etc etc
right??
|

May 22nd, 2007, 08:44 AM
|
Registered User
|
|
Join Date: May 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
So, I just keep the code for every match, but instead of using a messagebox for every match, it should just add a number to a counter, and do an If Then for a combination... THX!
|

May 22nd, 2007, 08:48 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
glad you work it out.. keep us informed about if you ended this ok...
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|
|
 |