winform小程序随机抽奖软件

下面介绍我做的一个winform程序,实现的结果是点击开始,然后名字一个一个地跳跃,然后点击介绍,名字停止跳动,此名字幸运的得了奖,而且不会重复名单!我准备用wpf来做的,虽然两者有很大的同共处,但是也有很微妙的区别。我做的这个软件在winform中需要timer控件,而在wpf中就没有timer控件了,需要DispatcherTimer这个类,而使用这个类的时候出现了很多问题,所以暂时先放置在了一边,用了winform窗体程序做了这个软件。此软件分了两个窗体,一个主窗体和一个子窗体,主窗体是抽奖用的,而子窗体是实现对名单的增删改查功能。

点击主窗体左上角的“查看人员名单”就会跳转到子窗体。

过程很简单

1.首先给主窗体添加一些控件,加一些label,button和listbox控件。label显示名字的跳跃,主窗体中的listbox将会获取得到的获奖名字,然后显示出来几等奖。“重新开始”是初始化,可以重新进行抽奖。

2.子窗体实现的是对名单的增删改查,也有一个文本导入名单功能。

3.然后就是主要的数据库操作了。我用的是sqlserver 2010数据库,我的想法是,这个软件需要两个表,一个表当然是储存名单用的,这个简单的软件只需要id和Name两个字段就行了。 另外一个表也有id和Name字段,初始是没有数据的,这个表是储存已经抽到人的名单。第二张表是对比第一张表的,抽奖的时候抽取第一张表中的名单,然后跟第二张表进行比对,如果有一样的就不显示,从而达到抽奖名单不重复的目的。

string sqlStr = "select Name, NewID() as random from T_Staff where Name not in (select Name from T_Staff1) order by random ";

4.上面的sql语言,放在timer里面,这样timer运行一次,此sql就运行一次。这样就实现了名字不断跳转的效果。

下面就把代码贴上,其实很简单。也有很多不完善的地方,将在本文最后总结中指出!

  1 public partial class Main : Form
  2     {
  3         public Main()
  4         {
  5             InitializeComponent();
  6         }
  7         public static string conStr = "uid=sa;pwd=123456;initial catalog=人员名单;data source=.;";
  8         public static SqlConnection conn = new SqlConnection(conStr);
  9         //int num = 0;
 10         private void timer1_Tick(object sender, EventArgs e)
 11         {
 12             try
 13             {
 14                 string sqlStr = "select Name, NewID() as random from T_Staff where Name not in (select Name from T_Staff1) order by random ";
 15                 SqlCommand cmd = new SqlCommand(sqlStr, conn);
 16                 conn.Open();
 17                 object obj = cmd.ExecuteScalar();
 18                 label2.Text = obj.ToString();
 19                 conn.Close();
 20             }
 21             catch 
 22             {
 23             }
 24         }
 25 
 26         private void Form1_Load(object sender, EventArgs e)
 27         {
 28             timer1.Interval = 40;
 29         }
 30         private void btn1_Click(object sender, EventArgs e)
 31         {
 32             label1.Text = "一等奖";
 33         }
 34 
 35         private void btn2_Click(object sender, EventArgs e)
 36         {
 37             label1.Text = "二等奖";
 38         }
 39 
 40         private void btn3_Click(object sender, EventArgs e)
 41         {
 42             label1.Text = "三等奖";
 43         }
 44         private void btnOther_Click(object sender, EventArgs e)
 45         {
 46             label1.Text = "其它奖项";
 47         }
 48         private void btnStart_Click(object sender, EventArgs e)
 49         {
 50             SoundPlayer player=new SoundPlayer();
 51             SoundPlayer player1 = new SoundPlayer();
 52             player.SoundLocation="E:/程序/抽奖系统2/抽奖系统2/Music/301.wav";
 53             player1.SoundLocation = "E:/程序/抽奖系统2/抽奖系统2/Music/3055.wav";
 54             SqlConnection conn = new SqlConnection(conStr);
 55             if (label1.Text == "欢迎")
 56             {
 57                 MessageBox.Show("请选择奖项!");
 58             }
 59             else if (btnStart.Text == "开始")
 60             {
 61                 player.PlayLooping();
 62                 btnStart.Text = "结束";
 63                 timer1.Start();
 64             }
 65             else
 66             {
 67                 player.Stop();
 68                 player1.Play();
 69                 timer1.Enabled = false;
 70                 string sqlStr1 = "Insert into T_Staff1(Name) values ('" + label2.Text + "')";
 71                 //num++;
 72                 SqlCommand com = new SqlCommand(sqlStr1, conn);
 73                 conn.Open();
 74                 com.ExecuteNonQuery();
 75                 conn.Close();
 76                 btnStart.Text = "开始";
 77                 if (label1.Text == "一等奖")
 78                 {
 79                     listBox1.Items.Add(label2.Text);
 80                 }
 81                 else if (label1.Text == "二等奖")
 82                 {
 83                     listBox2.Items.Add(label2.Text);
 84                 }
 85                 else if (label1.Text == "三等奖")
 86                 {
 87                     listBox3.Items.Add(label2.Text);
 88                 }
 89                 else if (label1.Text == "其它奖项")
 90                 {
 91                     listBox4.Items.Add(label2.Text);
 92                 }
 93             }
 94         }
 95         private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
 96         {
 97             SqlConnection conn = new SqlConnection(conStr);
 98             conn.Open();
 99             string sqlclear = "delete from T_Staff1";
100             SqlCommand com = new SqlCommand(sqlclear, conn);
101             com.ExecuteNonQuery();
102             conn.Close();
103             this.Close();
104         }
105         private void 重新开始ToolStripMenuItem_Click(object sender, EventArgs e)
106         {
107             SqlConnection conn = new SqlConnection(conStr);
108             conn.Open();
109             string sqlclear = "delete from T_Staff1";
110             SqlCommand com = new SqlCommand(sqlclear, conn);
111             com.ExecuteNonQuery();
112             conn.Close();
113             listBox1.Items.Clear();
114             listBox2.Items.Clear();
115             listBox3.Items.Clear();
116             listBox4.Items.Clear();
117             label1.Text = "欢迎";
118             label2.Text = "名单";
119             MessageBox.Show("已经初始化成功,请重新开始");
120         }
121         private void 查看人员名单ToolStripMenuItem_Click(object sender, EventArgs e)
122         {
123             NameList list = new NameList();
124             list.ShowDialog();
125         }      
126     }
主窗体后台代码
  1 public partial class NameList : Form
  2     {
  3         public NameList()
  4         {
  5             InitializeComponent();
  6         }
  7         public static string conStr = "uid=sa;pwd=123456;initial catalog=人员名单;data source=.;";
  8         public static SqlConnection conn = new SqlConnection(conStr);
  9         public void NameList_Load(object sender, EventArgs e)
 10         {
 11             conn.Open();
 12             //显示总共有多少的员工
 13             string strLong = "select count(*) from T_Staff";
 14             SqlCommand com = new SqlCommand(strLong, conn);
 15             int length=(int)com.ExecuteScalar();
 16             label1.Text = "总共有" + length + "个员工";
 17             //List显示数据库中的员工名单
 18             string strName = "select Name from T_Staff";
 19             DataSet ds = new DataSet();
 20             SqlDataAdapter adapter = new SqlDataAdapter(strName, conn);
 21             adapter.Fill(ds);
 22             foreach (DataRow row in ds.Tables[0].Rows)
 23             {
 24                 listBox1.Items.Add(row[0].ToString());
 25             }
 26             conn.Close();
 27         }
 28 
 29         private void button1_Click(object sender, EventArgs e)
 30         {
 31             //单值插入
 32             if (textBox1.Text == "")
 33             {
 34                 MessageBox.Show("请在上面输入名字");
 35             }
 36             else
 37             {
 38                 conn.Open();
 39                 string strInsert = "insert into T_Staff(Name) values('" + textBox1.Text + "')";
 40                 SqlCommand com = new SqlCommand(strInsert, conn);
 41                 com.ExecuteNonQuery();
 42                 MessageBox.Show(textBox1.Text + "插入成功");
 43                 conn.Close();             
 44             }
 45         }
 46 
 47         private void button6_Click(object sender, EventArgs e)
 48         {
 49             DialogResult dr = MessageBox.Show("你真的要全部删除吗?","删除操作",MessageBoxButtons.YesNo);
 50             if (dr == DialogResult.Yes)
 51             {
 52                 conn.Open();
 53                 string strDelete = "delete from T_Staff";
 54                 SqlCommand com = new SqlCommand(strDelete, conn);
 55                 com.ExecuteNonQuery();
 56                 conn.Close();
 57                 MessageBox.Show("删除成功");
 58             }
 59             else
 60             {
 61                 return;
 62             }
 63         }
 64 
 65         private void button2_Click(object sender, EventArgs e)
 66         {
 67             //选择性删除ListBox中的数据
 68             if (listBox1.SelectedItem != null)
 69             {
 70                 string selectedName = listBox1.SelectedItem.ToString();
 71                 conn.Open();
 72                 string strDel = "delete from T_Staff where Name='" + selectedName + "'";
 73                 SqlCommand com = new SqlCommand(strDel, conn);
 74                 com.ExecuteNonQuery();
 75                 conn.Close();
 76                 MessageBox.Show("删除成功");
 77             }
 78             else
 79             {
 80                 MessageBox.Show("请在左边选择名字");
 81             }
 82         }
 83 
 84         private void button4_Click(object sender, EventArgs e)
 85         {
 86             this.Close();
 87         }
 88 
 89         private void button5_Click(object sender, EventArgs e)
 90         {
 91             OpenFileDialog ofd = new OpenFileDialog();
 92             ofd.Filter = "文本文件|*.txt";
 93             ofd.ShowDialog();
 94             string filename = ofd.FileName;
 95             IEnumerable<string> lines= File.ReadAllLines(filename,Encoding.Default);
 96             foreach (string line in lines)
 97             {
 98                 string[] segs = line.Split(' ');
 99                 string name = segs[0];
100                 conn.Open();
101                 string strDel = "insert into T_Staff(Name) values('" + name + "')";
102                 SqlCommand com = new SqlCommand(strDel, conn);
103                 com.ExecuteNonQuery();
104                 conn.Close();
105             }
106             MessageBox.Show("成功导入"+lines.Count()+"条数据");
107         }
108     }
子窗体代码

这个小程序的问题很多。代码中的注释很很少,是当初写的时候没注意吧。窗体没有美化,没有太多时间去弄这个了。写这种对sql操作的时候应该把这些写到一个类中SqlHelper,这样直接调用这个类中的一些方法就行了,当初没注意,导致写的时候程序语句很冗杂。

下面是这个程序运行的效果

原文地址:https://www.cnblogs.com/xijianyao/p/3129400.html