| Printable Version
Lotto Application in C#
By Huseyin Altindag
Introduction
This small Lotto-Program helps you fill your lotto ticket.
It will display for each draw the 6 numbers in the 6 TextBoxes.
Every time you click "Next Draw >" button the numbers are displayed first unsorted
but by clicking the "Sort numbers" button they will be then displayed sorted in the TextBoxes.
How it works and the code
After the programm is started you get the 6 numbers displayed unsorted.
If you click the button "Sort numbers", all the 6 unsorted numbers will be sorted
And at the same time you see the actual draw number.
To sort the numbers, I use a temporary array.
The numbers in TextBoxes normally are stored as Text.
I convert those Text-values to Integer and assign the new integer
to the temporary array.
nr[0]=Convert.ToInt16(this.textBox1.Text);
After that I sort the numbers and assign the 6 numbers
from the new array nr to all TextBoxes and display the numbers.
The button "Sort numbers" will be disabled and
the Label "sorted" will be invisible.
Array.Sort(nr);
this.textBox1.Text = nr[0].ToString();
this.pbSort.Enabled=false;
this.labelSort.Visible=true;
Here is the whole code
/*-----------------------------------------------------------------------------
* Author :H|seyin Altindag
* Created :17/02/2004
* Changed : 03/08/2004
* Purpose : This program will show on a Form the 6 lotto numbers
------------------------------------------------------------------------------*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MyLotteryProject
{
///
/// Summary description for Form1.
///
public class MyLotteryForm : System.Windows.Forms.Form
{
///
/// Required designer variable.
///
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox6;
//how many draw?
public static int n_draw=1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label labelDaytime;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
//store the 6 random numbers
public int[] lottonumbers = new int[50];
public int[] result =
new int[50];
private System.Windows.Forms.Button pbSort;
private System.Windows.Forms.Button pbEnd;
private System.Windows.Forms.Button pbNextDraw;
private System.Windows.Forms.Label labelSort;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem miAbout;
private System.Windows.Forms.MenuItem miExit;
private System.Windows.Forms.MenuItem miSeperator;
private System.ComponentModel.Container components =
null;
public MyLotteryForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
/*----------------------------------------------------------
* this enables to use ESC-key for closing/end the program
*-----------------------------------------------------------*/
this.CancelButton=this.pbEnd;
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.pbNextDraw = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.labelDaytime = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.pbEnd = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.pbSort = new System.Windows.Forms.Button();
this.labelSort = new System.Windows.Forms.Label();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.miAbout = new System.Windows.Forms.MenuItem();
this.miSeperator = new System.Windows.Forms.MenuItem();
this.miExit = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.BackColor =
System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(0)));
this.textBox1.Location = new System.Drawing.Point(96, 160);
this.textBox1.Name =
"textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(24, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.BackColor = System.Drawing.Color.Gold;
this.textBox2.Location = new System.Drawing.Point(128, 160);
this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true;
this.textBox2.Size = new System.Drawing.Size(24, 20);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "textBox2";
//
// textBox3
//
this.textBox3.BackColor = System.Drawing.Color.Cyan;
this.textBox3.Location = new System.Drawing.Point(160, 160);
this.textBox3.Name = "textBox3";
this.textBox3.ReadOnly = true;
this.textBox3.Size = new System.Drawing.Size(24, 20);
this.textBox3.TabIndex = 2;
this.textBox3.Text = "textBox3";
//
// textBox4
//
this.textBox4.BackColor = System.Drawing.Color.Red;
this.textBox4.Location = new System.Drawing.Point(192, 160);
this.textBox4.Name = "textBox4";
this.textBox4.ReadOnly = true;
this.textBox4.Size = new System.Drawing.Size(24, 20);
this.textBox4.TabIndex = 3;
this.textBox4.Text = "textBox4";
//
// textBox5
//
this.textBox5.BackColor = System.Drawing.Color.Yellow;
this.textBox5.Location = new System.Drawing.Point(224, 160);
this.textBox5.Name = "textBox5";
this.textBox5.ReadOnly = true;
this.textBox5.Size = new System.Drawing.Size(24, 20);
this.textBox5.TabIndex = 4;
this.textBox5.Text = "textBox5";
//
// textBox6
//
this.textBox6.BackColor = System.Drawing.Color.Lime;
this.textBox6.Location = new System.Drawing.Point(256, 160);
this.textBox6.Name = "textBox6";
this.textBox6.ReadOnly = true;
this.textBox6.Size = new System.Drawing.Size(24, 20);
this.textBox6.TabIndex = 5;
this.textBox6.Text = "textBox6";
//
// pbNextDraw
//
this.pbNextDraw.BackColor =
System.Drawing.SystemColors.Control;
this.pbNextDraw.Cursor =
System.Windows.Forms.Cursors.Hand;
this.pbNextDraw.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.pbNextDraw.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.pbNextDraw.ForeColor = System.Drawing.Color.Black;
this.pbNextDraw.Location = new System.Drawing.Point(32, 208);
this.pbNextDraw.Name = "pbNextDraw";
this.pbNextDraw.Size = new System.Drawing.Size(136, 56);
this.pbNextDraw.TabIndex = 6;
this.pbNextDraw.Text = "Next Draw >";
this.pbNextDraw.Click += new System.EventHandler(this.pbNextDraw_Click);
//
// label1
//
this.label1.BackColor = System.Drawing.Color.Coral;
this.label1.Font = new System.Drawing.Font("Comic Sans MS", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(32, 8);
this.label1.Name =
"label1";
this.label1.Size = new System.Drawing.Size(328, 40);
this.label1.TabIndex =
7;
this.label1.Text = "Lottery Special Draw";
this.label1.TextAlign =
System.Drawing.ContentAlignment.TopCenter;
//
// label2
//
this.label2.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label2.ForeColor = System.Drawing.Color.IndianRed;
this.label2.Location = new System.Drawing.Point(32, 112);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(160, 23);
this.label2.TabIndex = 8;
this.label2.Text = "Winning Numbers for";
//
// labelDaytime
//
this.labelDaytime.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.labelDaytime.ForeColor =
System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(0)));
this.labelDaytime.Location = new System.Drawing.Point(192, 104);
this.labelDaytime.Name =
"labelDaytime";
this.labelDaytime.Size = new System.Drawing.Size(160, 40);
this.labelDaytime.TabIndex = 9;
this.labelDaytime.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// label3
//
this.label3.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label3.Location = new System.Drawing.Point(32, 72);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 23);
this.label3.TabIndex = 10;
this.label3.Text = "Draw nr.";
this.label3.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// label4
//
this.label4.BackColor = System.Drawing.Color.Bisque;
this.label4.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label4.ForeColor = System.Drawing.Color.Red;
this.label4.Location = new System.Drawing.Point(96, 72);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(40, 24);
this.label4.TabIndex = 11;
this.label4.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// pbEnd
//
this.pbEnd.BackColor =
System.Drawing.SystemColors.Control;
this.pbEnd.Cursor =
System.Windows.Forms.Cursors.Hand;
this.pbEnd.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.pbEnd.ForeColor =
System.Drawing.SystemColors.ControlText;
this.pbEnd.Location = new System.Drawing.Point(280, 224);
this.pbEnd.Name = "pbEnd";
this.pbEnd.Size = new System.Drawing.Size(80, 23);
this.pbEnd.TabIndex = 12;
this.pbEnd.Text = "End";
this.pbEnd.Click += new System.EventHandler(this.pbEnd_Click);
//
// label5
//
this.label5.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.label5.Font = new System.Drawing.Font("Times New Roman", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label5.Location = new System.Drawing.Point(0, 288);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(392, 23);
this.label5.TabIndex = 13;
this.label5.Text = "I Wish You Good Luck. Fingers Crossed !";
//
// pbSort
//
this.pbSort.BackColor =
System.Drawing.SystemColors.Control;
this.pbSort.Cursor = System.Windows.Forms.Cursors.Hand;
this.pbSort.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.pbSort.ForeColor =
System.Drawing.SystemColors.ControlText;
this.pbSort.Location = new System.Drawing.Point(184, 224);
this.pbSort.Name = "pbSort";
this.pbSort.Size = new System.Drawing.Size(80, 23);
this.pbSort.TabIndex = 14;
this.pbSort.Text = "Sort numbers";
this.pbSort.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
this.pbSort.Click += new System.EventHandler(this.pbSort_Click);
//
// labelSort
//
this.labelSort.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.labelSort.Location = new System.Drawing.Point(32, 160);
this.labelSort.Name = "labelSort";
this.labelSort.Size = new System.Drawing.Size(56, 23);
this.labelSort.TabIndex = 15;
this.labelSort.Text = "Sorted:";
this.labelSort.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.miAbout,
this.miSeperator,
this.miExit});
this.menuItem1.Text = "File";
//
// miAbout
//
this.miAbout.Index = 0;
this.miAbout.Text = "About..";
this.miAbout.Click += new System.EventHandler(this.miAbout_Click);
//
// miSeperator
//
this.miSeperator.Index = 1;
this.miSeperator.Text = "-";
//
// miExit
//
this.miExit.Index = 2;
this.miExit.Text = "Exit";
this.miExit.Click += new System.EventHandler(this.pbEnd_Click);
//
// MyLotteryForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(390, 315);
this.Controls.Add(this.labelSort);
this.Controls.Add(this.pbSort);
this.Controls.Add(this.label5);
this.Controls.Add(this.pbEnd);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.labelDaytime);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.pbNextDraw);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.ForeColor = System.Drawing.Color.IndianRed;
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "MyLotteryForm";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Lottery-Form";
this.Load += new System.EventHandler(this.MyLotteryForm_Load);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MyLotteryForm());
}
private void MyLotteryForm_Load(object sender, System.EventArgs e)
{
//this algorithm comes originally from Internet
//-------------------- Beginning of the algorithm ------------------
int c=1000;
int[] numb = new int[c];
DateTime dt = new DateTime();
dt = DateTime.Now;
Random rnd = new Random(dt.Millisecond);
for (int i=1; i
{
numb[ i ] = rnd.Next(1,50);
}
Array.Sort(numb);
int k=0;
int j=0;
for (int i=0; i
{
if (numb[ i ] ==
numb[i+1])
{
result[k] = numb[ i ];
lottonumbers[k] = j+=1;
}
else
{
k+=1;
j=0;
}
}
//-----------------End of algorithm ----------------------
Array.Sort(lottonumbers, result);
// first display numbers unsorted
fnDisplay6NumbersUnsorted();
//get the date of today
DateTime dt1= DateTime.Today;
//display date
this.labelDaytime.Text=dt1.ToLongDateString();
//display which draw number
this.label4.Text=n_draw.ToString();
//don4t show text "sorted:"
this.labelSort.Visible=false;
this.pbNextDraw.Focus();
}
private void fnDisplay6NumbersUnsorted()
{
// it displays the 6 lotto numbers in the TextBoxes
this.textBox1.Text =
result[49].ToString();
this.textBox2.Text =
result[48].ToString();
this.textBox3.Text =
result[47].ToString();
this.textBox4.Text =
result[46].ToString();
this.textBox5.Text =
result[45].ToString();
this.textBox6.Text =
result[44].ToString();
}
private void fnDisplay6NumbersSorted()
{
/*when user clicks push button("sort numbers")
* convert all the numbers in textboxes as Text to Integer
* sort them and assign the integer numbers from the array
* to the textboxes and display
*/
//temporary array
int[] nr = new int[6];
//put all 6 numbers from TextBoxes into one array
nr[0]=Convert.ToInt16(this.textBox1.Text);
nr[1]=Convert.ToInt16(this.textBox2.Text);
nr[2]=Convert.ToInt16(this.textBox3.Text);
nr[3]=Convert.ToInt16(this.textBox4.Text);
nr[4]=Convert.ToInt16(this.textBox5.Text);
nr[5]=Convert.ToInt16(this.textBox6.Text);
//sort the new array nr
Array.Sort(nr);
//now assign the 6 numbers from the new array nr to all TextBoxes and display
this.textBox1.Text = nr[0].ToString();
this.textBox2.Text = nr[1].ToString();
this.textBox3.Text = nr[2].ToString();
this.textBox4.Text = nr[3].ToString();
this.textBox5.Text = nr[4].ToString();
this.textBox6.Text = nr[5].ToString();
}
private void pbNextDraw_Click(object sender, System.EventArgs e)
{
this.MyLotteryForm_Load(sender, e);
//draw number one more
n_draw++;
//display draw number in textbox
this.label4.Text=n_draw.ToString();
//enable the sort pushbutton
this.pbSort.Enabled=true;
}
private void pbEnd_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void pbSort_Click(object sender, System.EventArgs e)
{
//call the methode to sort
fnDisplay6NumbersSorted();
//disable the sort pushbutton
this.pbSort.Enabled=false;
//enable the sort Label "sorted"
this.labelSort.Visible=true;
this.pbNextDraw.Focus();
}
private void miAbout_Click(object sender, System.EventArgs e)
{
//show the Form "FormAbout"
FormAbout frm=new FormAbout();
frm.ShowDialog();
}
} //class MyLotteryForm
} //namespace MyLotteryProject
MyLotteryProject.zip
I sometimes run this program and jot down the numbers and play but
except 3 right numbers I haven4t won millions yet. Should you win millions
through this draw please let me know about it !
Good luck and enjoy
H|seyin Altindag
|