1. Create a GUI lottery game application. Generate three random numbers, each between 1 and 4. Allow the user to guess three numbers. Compare each of the user's guesses to the three random numbers and display a message that includes the user's guess, the randomly deter-mined three-digit number, and the amount of money the user has won as follows:
Matching Numbers Award ($)
Any one matching 10
Two matching 100
Three matching, not in order 1000
Three matching in exact order 10,000
No matches 0
Make certain that your application accommodates repeating digits. For example, if a user guesses 1, 2, and 3, and the randomly generated digits are 1, 1, and 1, do not give the user credit for three correct guesses—just one.
--------------------------------------------------------------------------------------------------------------
Errors : i create this GUI lottery program it just read textbox1.text value and give me only first value 3 time in output.... need a help to fix it ? File Attached for problem understanding.
--------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace LotteryGUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// declaring integers
int iNum1 = Convert.ToInt32(textBox1.Text);
int iNum2 = Convert.ToInt32(textBox2.Text);
int iNum3 = Convert.ToInt32(textBox3.Text);
int iCount;
int iGuess;
// declaring constants
const int iBALLS = 3;
const int iMATCHONE = 10;
const int iMATCHTWO = 100;
const int iMATCHTHREE = 1000;
const int iMATCHFOUR = 10000;
// declaring arrays
int[] iSortedNums = new int[3];
int[] iGuesses = new int[3];
int[] iSortedGuesses = new int[3];
// generating random numbers
Random randomnumber = new Random();
iNum1 = randomnumber.Next(1, 5); // gives number between 1 and 4
iNum2 = randomnumber.Next(1, 5);
iNum3 = randomnumber.Next(1, 5);
iSortedNums[0] = iNum1;
iSortedNums[1] = iNum2;
iSortedNums[2] = iNum3;
Array.Sort(iSortedNums); // sort random numbers
for (iCount = 0; iCount < iBALLS; iCount++)
{
//textBox1.Text=("No:" + (iCount + 1) + " Choose your number: ");
iGuess = Convert.ToInt32(textBox1.Text);
while (iGuess < 1 || iGuess > 4)
{
label6.Text = ("You have entered an incorrect number please try again");
textBox1.Text=("\nNo:" + (iCount + 1) + " Choose your number: ");
iGuess = Convert.ToInt32(Console.ReadLine());
}
iGuesses[iCount] = iGuess;
iSortedGuesses[iCount] = iGuess;
}
Array.Sort(iSortedGuesses); // sort the guesses
label4.Text =String.Format ("The random numbers are : " + iNum1 + ", " + iNum2 + ", " + iNum3);
label5.Text = String.Format("The numbers you chose are : " + iGuesses[0] + ", " + iGuesses[1] + ", " + iGuesses[2]);
if (iGuesses[0] == iNum1 && iGuesses[1] == iNum2 && iGuesses[2] == iNum3)
{
MessageBox.Show("you won $" + iMATCHFOUR);
}
else if (iSortedGuesses[0] == iSortedNums[0] && iSortedGuesses[1] == iSortedNums[1]
&& iSortedGuesses[2] == iSortedNums[2])
{
MessageBox.Show("you won $" + iMATCHTHREE);
}
else if ((iGuesses[0] == iNum1 && iGuesses[1] == iNum2) || (iGuesses[1] == iNum2 &&
iGuesses[2] == iNum3))
{
MessageBox.Show("you won $" + iMATCHTWO);
}
else if (iGuesses[0] == iNum1 || iGuesses[1] == iNum2 || iGuesses[2] == iNum3)
{
MessageBox.Show("you won $" + iMATCHONE);
}
else
{
MessageBox.Show("sorry, you didn't win anything");
}
}
}
}
Loading

VulpesPosted Aug 11, 2012, 1:27 PM
The problem is that, when you do this, the eventhandlers are not wired up to the events and so when the events occur, the eventhanders fail to execute.
To avoid this, it's usually best to find the event in the Properties Box for the relevant control and double click it. This opens up a skeleton eventhandler for the event concerned, for example:
}
However, it also adds a line of code to a hidden method called InitializeComponent which you might have noticed is always called from the form's constructor. This line of code looks like this and 'wires up' the event to the handler:
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
So, if you always add your eventhandlers in this fashion, then they will automatically be wired up to the events. You can then add or paste your code into the skeleton handler.
You could, of course, wire up events yourself by adding a line such as the above to (say) the form's constructor (you shouldn't edit the InitializeComponent method manually) and this is necessary when controls are being added dynamically at runtime rather than at compile time.
However, it's easy to get these wrong and so it's usually best to let the VS designer add them for you as I said earlier.
I suspect that you may have simply pasted the Leave eventhandlers into your program which would explain why the guesses are always shown as zero, as these are the default values for the integer array, iGuesses. These are the values they will have if the Leave eventhandlers don't execute.
So, I'd check that out and, if necessary, correct it. The program is working fine at my end.
LotusPosted Aug 11, 2012, 3:31 PM
LotusPosted Aug 11, 2012, 8:58 AM
dear it cannot read value from textbox when i run application it show me result "The numbers you chose are : 0, 0, 0 "
please also explain this little more i cant understand...
"The 'skeleton' Leave eventhandlers for the 3 textboxes should, of course, be added by double-clicking on them in the Properties Box."
VulpesPosted Aug 11, 2012, 7:00 AM
Here's my attempt to make it work. The 'skeleton' Leave eventhandlers for the 3 textboxes should, of course, be added by double-clicking on them in the Properties Box.
If I were you, I'd get rid of label6 and use MessageBox's instead: