简单的抽奖器

公司元旦的时候搞了一个小小的抽奖器,我做的,抽奖的时候心情和别人就是不一样!人家盼着抽到大奖,我可是想着最好别到我,痛苦啊!!!!
不过我们写程序的能有点编写的快乐就够了赫赫!
很简单的东西,一个随机函数从数据库中选区员工编号,然后抽到的时候给他们做上标号,以免重复抽取。
代码如下:(代码占不了只能用文本,“{}”用“[]”代替)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Data.OleDb;

namespace winit
[
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 [
  private System.Windows.Forms.Timer timer1;
  private System.Windows.Forms.Label label1;
  private System.ComponentModel.IContainer components;
  //static int alarmCounter = 1;
  string gLinkString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source='db.mdb'";
  private System.Windows.Forms.Button button1;
  static string exitFlag = "OK";
  private System.Windows.Forms.RadioButton prize1;
  private System.Windows.Forms.RadioButton prize2;
  private System.Windows.Forms.RadioButton prize3;
  private System.Windows.Forms.RadioButton prize4;
  private System.Windows.Forms.RadioButton prize5;
  private System.Windows.Forms.Label temp_id;
  private System.Windows.Forms.ListBox prize2_lst;
  private System.Windows.Forms.ListBox prize3_lst;
  private System.Windows.Forms.ListBox prize4_lst;
  private System.Windows.Forms.ListBox prize5_lst;
  private System.Windows.Forms.ListBox prize1_lst;
  private System.Windows.Forms.RadioButton prize6;
  private System.Windows.Forms.ListBox prize6_lst;
  private System.Windows.Forms.Label temp_name;
  //static Random rdm1;

  public Form1()
  [
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  ]

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  [
   if( disposing )
   [
    if (components != null)
    [
     components.Dispose();
    ]
   ]
   base.Dispose( disposing );
  ]

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  [
   this.components = new System.ComponentModel.Container();
   System.Resources.ResourceManager resources = new

System.Resources.ResourceManager(typeof(Form1));
   this.timer1 = new System.Windows.Forms.Timer(this.components);
   this.label1 = new System.Windows.Forms.Label();
   this.button1 = new System.Windows.Forms.Button();
   this.prize1 = new System.Windows.Forms.RadioButton();
   this.prize2 = new System.Windows.Forms.RadioButton();
   this.prize3 = new System.Windows.Forms.RadioButton();
   this.prize4 = new System.Windows.Forms.RadioButton();
   this.prize5 = new System.Windows.Forms.RadioButton();
   this.temp_id = new System.Windows.Forms.Label();
   this.temp_name = new System.Windows.Forms.Label();
   this.prize2_lst = new System.Windows.Forms.ListBox();
   this.prize3_lst = new System.Windows.Forms.ListBox();
   this.prize4_lst = new System.Windows.Forms.ListBox();
   this.prize5_lst = new System.Windows.Forms.ListBox();
   this.prize1_lst = new System.Windows.Forms.ListBox();
   this.prize6_lst = new System.Windows.Forms.ListBox();
   this.prize6 = new System.Windows.Forms.RadioButton();
   this.SuspendLayout();
   //
   // timer1
   //
   this.timer1.Enabled = true;
   this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
   //
   // label1
   //
   this.label1.BackColor = System.Drawing.Color.White;
   this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif",

30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)

(134)));
   this.label1.Location = new System.Drawing.Point(64, 144);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(296, 56);
   this.label1.TabIndex = 0;
   this.label1.Text = "label1";
   //
   // button1
   //
   this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif",

15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)

(134)));
   this.button1.Location = new System.Drawing.Point(392, 152);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(104, 40);
   this.button1.TabIndex = 1;
   this.button1.Text = "Stop";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // prize1
   //
   this.prize1.BackColor = System.Drawing.Color.Transparent;
   this.prize1.Font = new System.Drawing.Font("Microsoft Sans Serif",

9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)

(0)));
   this.prize1.ForeColor = System.Drawing.Color.Red;
   this.prize1.Location = new System.Drawing.Point(48, 216);
   this.prize1.Name = "prize1";
   this.prize1.Size = new System.Drawing.Size(72, 24);
   this.prize1.TabIndex = 3;
   this.prize1.Text = "一等奖";
   //
   // prize2
   //
   this.prize2.BackColor = System.Drawing.Color.Transparent;
   this.prize2.Font = new System.Drawing.Font("Microsoft Sans Serif",

9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)

(0)));
   this.prize2.ForeColor = System.Drawing.Color.Red;
   this.prize2.Location = new System.Drawing.Point(160, 216);
   this.prize2.Name = "prize2";
   this.prize2.Size = new System.Drawing.Size(72, 24);
   this.prize2.TabIndex = 4;
   this.prize2.Text = "二等奖";
   //
   // prize3
   //
   this.prize3.BackColor = System.Drawing.Color.Transparent;
   this.prize3.Font = new System.Drawing.Font("Microsoft Sans Serif",

9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)

(0)));
   this.prize3.ForeColor = System.Drawing.Color.Red;
   this.prize3.Location = new System.Drawing.Point(248, 216);
   this.prize3.Name = "prize3";
   this.prize3.Size = new System.Drawing.Size(72, 24);
   this.prize3.TabIndex = 5;
   this.prize3.Text = "三等奖";
   //
   // prize4
   //
   this.prize4.BackColor = System.Drawing.Color.Transparent;
   this.prize4.Font = new System.Drawing.Font("Microsoft Sans Serif",

9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)

(0)));
   this.prize4.ForeColor = System.Drawing.Color.Red;
   this.prize4.Location = new System.Drawing.Point(352, 216);
   this.prize4.Name = "prize4";
   this.prize4.Size = new System.Drawing.Size(72, 24);
   this.prize4.TabIndex = 6;
   this.prize4.Text = "四等奖";
   //
   // prize5
   //
   this.prize5.BackColor = System.Drawing.Color.Transparent;
   this.prize5.Font = new System.Drawing.Font("Microsoft Sans Serif",

9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)

(0)));
   this.prize5.ForeColor = System.Drawing.Color.Red;
   this.prize5.Location = new System.Drawing.Point(456, 216);
   this.prize5.Name = "prize5";
   this.prize5.Size = new System.Drawing.Size(72, 24);
   this.prize5.TabIndex = 7;
   this.prize5.Text = "五等奖";
   //
   // temp_id
   //
   this.temp_id.BackColor = System.Drawing.Color.Firebrick;
   this.temp_id.Location = new System.Drawing.Point(456, 8);
   this.temp_id.Name = "temp_id";
   this.temp_id.TabIndex = 8;
   this.temp_id.Text = "label3";
   this.temp_id.Visible = false;
   //
   // temp_name
   //
   this.temp_name.BackColor = System.Drawing.Color.Firebrick;
   this.temp_name.Location = new System.Drawing.Point(40, 8);
   this.temp_name.Name = "temp_name";
   this.temp_name.TabIndex = 9;
   this.temp_name.Text = "label3";
   this.temp_name.Visible = false;
   //
   // prize2_lst
   //
   this.prize2_lst.BackColor = System.Drawing.Color.White;
   this.prize2_lst.Location = new System.Drawing.Point(152, 264);
   this.prize2_lst.Name = "prize2_lst";
   this.prize2_lst.Size = new System.Drawing.Size(56, 342);
   this.prize2_lst.TabIndex = 10;
   //
   // prize3_lst
   //
   this.prize3_lst.BackColor = System.Drawing.Color.FromArgb

(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(192)));
   this.prize3_lst.Location = new System.Drawing.Point(256, 264);
   this.prize3_lst.Name = "prize3_lst";
   this.prize3_lst.Size = new System.Drawing.Size(56, 342);
   this.prize3_lst.TabIndex = 11;
   //
   // prize4_lst
   //
   this.prize4_lst.BackColor = System.Drawing.Color.Orange;
   this.prize4_lst.Location = new System.Drawing.Point(352, 264);
   this.prize4_lst.Name = "prize4_lst";
   this.prize4_lst.Size = new System.Drawing.Size(56, 342);
   this.prize4_lst.TabIndex = 12;
   //
   // prize5_lst
   //
   this.prize5_lst.BackColor = System.Drawing.Color.FromArgb

(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
   this.prize5_lst.Location = new System.Drawing.Point(448, 264);
   this.prize5_lst.Name = "prize5_lst";
   this.prize5_lst.Size = new System.Drawing.Size(56, 342);
   this.prize5_lst.TabIndex = 13;
   //
   // prize1_lst
   //
   this.prize1_lst.BackColor = System.Drawing.Color.PaleGreen;
   this.prize1_lst.Location = new System.Drawing.Point(56, 264);
   this.prize1_lst.Name = "prize1_lst";
   this.prize1_lst.Size = new System.Drawing.Size(56, 342);
   this.prize1_lst.TabIndex = 14;
   //
   // prize6_lst
   //
   this.prize6_lst.BackColor = System.Drawing.Color.Aquamarine;
   this.prize6_lst.Location = new System.Drawing.Point(552, 264);
   this.prize6_lst.Name = "prize6_lst";
   this.prize6_lst.Size = new System.Drawing.Size(56, 342);
   this.prize6_lst.TabIndex = 16;
   //
   // prize6
   //
   this.prize6.BackColor = System.Drawing.Color.Transparent;
   this.prize6.Font = new System.Drawing.Font("Microsoft Sans Serif",

9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)

(0)));
   this.prize6.ForeColor = System.Drawing.Color.Red;
   this.prize6.Location = new System.Drawing.Point(544, 216);
   this.prize6.Name = "prize6";
   this.prize6.Size = new System.Drawing.Size(72, 24);
   this.prize6.TabIndex = 15;
   this.prize6.Text = "六等奖";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject

("$this.BackgroundImage")));
   this.ClientSize = new System.Drawing.Size(648, 645);
   this.Controls.Add(this.prize6_lst);
   this.Controls.Add(this.prize6);
   this.Controls.Add(this.prize1_lst);
   this.Controls.Add(this.prize5_lst);
   this.Controls.Add(this.prize4_lst);
   this.Controls.Add(this.prize3_lst);
   this.Controls.Add(this.prize2_lst);
   this.Controls.Add(this.temp_name);
   this.Controls.Add(this.temp_id);
   this.Controls.Add(this.prize5);
   this.Controls.Add(this.prize4);
   this.Controls.Add(this.prize3);
   this.Controls.Add(this.prize2);
   this.Controls.Add(this.prize1);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.label1);
   this.Name = "Form1";
   this.Text = "    this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  ]
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  [
   
Application.Run(new Form1());
  ]


  private void Form1_Load(object sender, System.EventArgs e)
  [
   this.loadprize("prize1",prize1_lst);
   this.loadprize("prize2",prize2_lst);
   this.loadprize("prize3",prize3_lst);
   this.loadprize("prize4",prize4_lst);
   this.loadprize("prize5",prize5_lst);
   this.loadprize("prize6",prize6_lst);
   //this.label1.Text="dd";
   timer1.Interval = 100;
   timer1.Start();

 
   // Runs the timer, and raises the event.
   //while(alarmCounter == 100)
   //[
    
   //Application.DoEvents();
   //]
  ]

  private void timer1_Tick(object sender, System.EventArgs e)
  [
   //timer1.Stop();
   Random rdm1=new Random();
   //this.label1.Text=this.getname(rdm1.Next(1,10).ToString());
   try
   [
    OleDbDataAdapter goodsAdp = null;
    DataSet goodsDataSet = null;
    
    string sqlSelectStr = "select * from name_table where

id="+rdm1.Next(1,108).ToString()+" and prize='none'";
    goodsAdp = new OleDbDataAdapter(sqlSelectStr,gLinkString);
    goodsDataSet = new DataSet();
    goodsAdp.Fill(goodsDataSet,"Orders");
    DataTable tbl= goodsDataSet.Tables["Orders"];
    if(!tbl.Rows.Count.Equals(0))
    [
     this.label1.Text = tbl.Rows[0].ItemArray

[1].ToString()+"   "+tbl.Rows[0].ItemArray[2].ToString();
     this.temp_id.Text=tbl.Rows[0].ItemArray[0].ToString

();
     this.temp_name.Text=tbl.Rows[0].ItemArray

[2].ToString();
    ]
    else
    [
     timer1.Stop();
     timer1.Interval=1;
     this.label1.Text=rdm1.Next(40,80).ToString();
     this.temp_id.Text="None";
     this.temp_name.Text="None";
     //this.getname(rdm1.Next(20,40).ToString());
     
     timer1.Start();
    ]

   
   ]
   catch(Exception ex)
   [
    MessageBox.Show(ex.Message.ToString(), "Name Entry Error", 

MessageBoxButtons.OK);

   ]

   //rdm1.Next(1,100);
   
   //rdm1.Next(1,100);

//   alarmCounter +=1;
//   if(alarmCounter==100)
//   [
//    alarmCounter=1;
//   ]

   //timer1.Enabled = true;
   
     
//   if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter,
//    MessageBoxButtons.YesNo) == DialogResult.Yes)
//   [
//    
//    alarmCounter +=1;
//    timer1.Enabled = true;
//   ]
//   else
//   [
//    // Stops the timer.
//    exitFlag = true;
//   ]

  ]
  public void loadprize(string prizestr,System.Windows.Forms.ListBox 

prize_lst)
  [int i=0;
   OleDbDataAdapter goodsAdp = null;
   DataSet goodsDataSet = null;
   //goodsConnection.Open();
   string sqlSelectStr = "select * from name_table where

prize='"+prizestr+"' order by sj";
   goodsAdp = new OleDbDataAdapter(sqlSelectStr,gLinkString);
   goodsDataSet = new DataSet();
   goodsAdp.Fill(goodsDataSet,"Orders");
   DataTable tbl= goodsDataSet.Tables["Orders"];
   //prize1_lst.Text=tbl.Rows[0].ItemArray[1].ToString();
   if(tbl.Rows.Count.Equals(0))
   [
    prize_lst.Items.Add("获奖名单");
   ]
   else
   [
   for(i=0;i<tbl.Rows.Count;i++)
   [
    prize_lst.Items.Add(tbl.Rows[i].ItemArray[2].ToString());
    
   ]
   ]


  
  ]
  public void getname(string e_no)
  [ 
   try
   [
   OleDbDataAdapter goodsAdp = null;
   DataSet goodsDataSet = null;
    
   string sqlSelectStr = "select * from name_table where

id="+e_no.ToString()+" and prize='none'";
   goodsAdp = new OleDbDataAdapter(sqlSelectStr,gLinkString);
   goodsDataSet = new DataSet();
   goodsAdp.Fill(goodsDataSet,"Orders");
   DataTable tbl= goodsDataSet.Tables["Orders"];
    if(!tbl.Rows.Count.Equals(0))
    [
     this.label1.Text = tbl.Rows[0].ItemArray

[1].ToString()+"   "+tbl.Rows[0].ItemArray[2].ToString();
     this.temp_id.Text=tbl.Rows[0].ItemArray[0].ToString

();
     this.temp_name.Text=tbl.Rows[0].ItemArray

[2].ToString();
    ]
    else[
     Random rdm2=new Random();
     this.getname(rdm2.Next(20,60).ToString());
    ]
   
   ]
   catch(Exception ex)
   [
    MessageBox.Show(ex.Message.ToString(), "Name Entry Error", 

MessageBoxButtons.OK);

   ]
 
   
  ]
  public int getspeed()
  [
   int speed=100;

   OleDbDataAdapter goodsAdp = null;
   DataSet goodsDataSet = null;
    
   string sqlSelectStr = "select * from name_table where 

prize='none'";
   goodsAdp = new OleDbDataAdapter(sqlSelectStr,gLinkString);
   goodsDataSet = new DataSet();
   goodsAdp.Fill(goodsDataSet,"Orders");
   DataTable tbl= goodsDataSet.Tables["Orders"];
   speed=tbl.Rows.Count;
   
   return speed;
  ]
  public void nameok(string eid,string prize)
  [
   try
   [
    //OleDbDataAdapter goodsAdp2 = null;
    //DataSet goodsDataSet2 = null;
    OleDbConnection myConnection = new OleDbConnection

(gLinkString);

    OleDbCommand mycommand=null;
    string sqlSelectStr2 = "update name_table set

prize='"+prize.ToString()+"',sj='"+DateTime.Now.ToString()+"' where id="+eid.ToString()+"";
     mycommand=new OleDbCommand(sqlSelectStr2,myConnection);
    mycommand.Connection.Open();
    mycommand.ExecuteNonQuery();
    mycommand.Connection.Close();
 
   
   ]
   catch(Exception ex)
   [
    MessageBox.Show(ex.Message.ToString(), "Name Entry Error", 

MessageBoxButtons.OK);

   ]
  ]


  private void button1_Click(object sender, System.EventArgs e)
  [
   if(exitFlag.Equals("OK"))
   [ 
    timer1.Stop();
    exitFlag="stopped";
    //this.nameok("dd");
    string winprize="";
    if(this.prize1.Checked==true)
    [
     winprize= "prize1";
    ]
    if(this.prize2.Checked==true)
    [
     winprize= "prize2";
    ]
    if(this.prize3.Checked==true)
    [
     winprize= "prize3";
    ]
    if(this.prize4.Checked==true)
    [
     winprize= "prize4";
    ]
    if(this.prize5.Checked==true)
    [
     winprize= "prize5";
    ]
    if(this.prize6.Checked==true)
    [
     winprize= "prize6";
    ]
    
    

      switch (winprize)
      [
       case "prize1":
       [
        if(this.temp_id.Text.Equals

("None"))
        [
         timer1.Start();
         exitFlag="OK";
         

this.button1.Text="Stop";
        ]
        else
        [
         MessageBox.Show("恭

喜获奖:"+this.temp_name.Text.ToString());
         this.nameok

(this.temp_id.Text.ToString(),winprize.ToString());
         

this.prize1_lst.Items.Add(this.temp_name.Text.ToString());
         

this.button1.Text="Start";
        ]

     
       break;
              ]

       case "prize2":
       [
        if(this.temp_id.Text.Equals

("None"))
        [
         timer1.Start();
         exitFlag="OK";
         

this.button1.Text="Stop";
         
        ]
        else
        [
         MessageBox.Show("恭

喜获奖:"+this.temp_name.Text.ToString());
         this.nameok

(this.temp_id.Text.ToString(),winprize.ToString());
         

this.prize2_lst.Items.Add(this.temp_name.Text.ToString());
         

this.button1.Text="Start";
        ]

     
        break;
       ]
       case "prize3":
       [
        if(this.temp_id.Text.Equals

("None"))
        [
         timer1.Start();
         exitFlag="OK";
         

this.button1.Text="Stop";
        ]
        else

        [


         MessageBox.Show("恭

喜获奖:"+this.temp_name.Text.ToString());
         this.nameok

(this.temp_id.Text.ToString(),winprize.ToString());
         

this.prize3_lst.Items.Add(this.temp_name.Text.ToString());
         

this.button1.Text="Start";
        

        ]

     
        break;
       ]

       case "prize4":
       [
        if(this.temp_id.Text.Equals

("None"))
        [
         timer1.Start();
         exitFlag="OK";
         

this.button1.Text="Stop";
        ]
        else
        [
         MessageBox.Show("恭

喜获奖:"+this.temp_name.Text.ToString());
         this.nameok

(this.temp_id.Text.ToString(),winprize.ToString());
         

this.prize4_lst.Items.Add(this.temp_name.Text.ToString());
         

this.button1.Text="Start";
        ]

     
        break;
       ]
       case "prize5":
       [
        if(this.temp_id.Text.Equals

("None"))
        [
         timer1.Start();
         exitFlag="OK";
         

this.button1.Text="Stop";
        ]
        else
        [
         MessageBox.Show("恭

喜获奖:"+this.temp_name.Text.ToString());
         this.nameok

(this.temp_id.Text.ToString(),winprize.ToString());
         

this.prize5_lst.Items.Add(this.temp_name.Text.ToString());
         

this.button1.Text="Start";
        ]

     
        break;
       ]
       case "prize6":
       [
        if(this.temp_id.Text.Equals

("None"))
        [
         timer1.Start();
         exitFlag="OK";
         

this.button1.Text="Stop";
        ]
        else
        [
         MessageBox.Show("恭

喜获奖:"+this.temp_name.Text.ToString());
         this.nameok

(this.temp_id.Text.ToString(),winprize.ToString());
         

this.prize6_lst.Items.Add(this.temp_name.Text.ToString());
         

this.button1.Text="Start";
        ]

     
        break;
       ]
       default:
       [
        MessageBox.Show(" 选择抽奖等

级!");
        break;
       ]

     ]
    timer1.Interval =100;


    //'MessageBox.Show(this.getspeed().ToString());

     

    

//    //alarmCounter=1;
//    int i;

//    OleDbDataAdapter goodsAdp = null;
//    DataSet goodsDataSet = null;
//    
//    string sqlSelectStr = "select * from name_table where

id="+this.label1.Text.ToString()+"";
//    goodsAdp = new OleDbDataAdapter(sqlSelectStr,gLinkString);
//    goodsDataSet = new DataSet();
//    goodsAdp.Fill(goodsDataSet,"Orders");
//    DataTable tbl= goodsDataSet.Tables["Orders"];
//    MessageBox.Show(this.getname("3"));
//    this.label2.Text=tbl.Rows[0].ItemArray[2].ToString();
    
    //label2.Text=;
//    for(i=0;i<tbl.Rows.Count;i++)
//    [
//     region.Items.Add(tbl.Rows[i].ItemArray[1]);
//    ]
   ]
   else
   [
    timer1.Start();
    exitFlag="OK";
    this.button1.Text="Stop";
    //alarmCounter=1;
   ]

  ]
 ]
]

原文地址:https://www.cnblogs.com/Mint/p/359787.html