Forum guidelines
AuthorQuestion
Create a GUI lottery Game application - Help me
Posted on: 10 Aug 2012
lotterygui.rar 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");
            }


        }
    }
}



AuthorReply
Vulpes
  • 0
  • 0
accepted
Re: Create a GUI lottery Game application - Help me
Posted on: 11 Aug 2012   Accepted Answer
A mistake which developers who are new to windows forms programming often make is to paste in entire eventhandlers.

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:

   private void textBox1_Leave(object sender, EventArgs e)
   {

   } 

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.


Re: Create a GUI lottery Game application - Help me
Posted on: 11 Aug 2012  
It's never straightforward converting a console application to a windows form application as you have to allow in the latter for events not necessarily happening in the order you'd expect.

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:

namespace LotteryGUI
{
    public partial class Form1 : Form
    {
        private int[] iGuesses = new int[3]; // this needs to be declared at form level
        private int[] iSortedGuesses = new int[3]; // ditto

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
            {
                MessageBox.Show("You haven't yet chosen three numbers");
                return;
            }

            // declaring integers

            int iNum1;
            int iNum2; 
            int iNum3;

            // declaring constants

            const int iMATCHONE = 10;
            const int iMATCHTWO = 100;
            const int iMATCHTHREE = 1000;
            const int iMATCHFOUR = 10000;

            // declaring array

            int[] iSortedNums = 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
            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");
            }          
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("You haven't chosen your first number yet");
                textBox1.Focus();
                return;
            }

            int iGuess;
            int.TryParse(textBox1.Text, out iGuess);

            if (iGuess < 1 || iGuess > 4)
            {
                MessageBox.Show("You have entered an incorrect number please try again");
                textBox1.Focus();
                return;
            }
            
            iGuesses[0] = iGuess;
            iSortedGuesses[0] = iGuess;
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
            {
                MessageBox.Show("You haven't chosen your second number yet");
                textBox2.Focus();
                return;
            }

            int iGuess;
            int.TryParse(textBox2.Text, out iGuess);

            if (iGuess < 1 || iGuess > 4)
            {
                MessageBox.Show("You have entered an incorrect number please try again");
                textBox2.Focus();
                return;
            }

            iGuesses[1] = iGuess;
            iSortedGuesses[1] = iGuess;
        }

        private void textBox3_Leave(object sender, EventArgs e)
        {
            if (textBox3.Text == "")
            {
                MessageBox.Show("You haven't chosen your third number yet");
                textBox3.Focus();
                return;
            }

            int iGuess;
            int.TryParse(textBox3.Text, out iGuess);

            if (iGuess < 1 || iGuess > 4)
            {
                MessageBox.Show("You have entered an incorrect number please try again");
                textBox3.Focus();
                return;
            }

            iGuesses[2] = iGuess;
            iSortedGuesses[2] = iGuess;
        }
    }
}






Re: Create a GUI lottery Game application - Help me
Posted on: 11 Aug 2012  
thanks   Vulpes for helping me ...
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." 





Re: Create a GUI lottery Game application - Help me
Posted on: 11 Aug 2012  
thanks for help dear friend Vulpes i did it and now its working fine ....

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.