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/archive/2013/06/09/3129400.html
總結
以上是生活随笔為你收集整理的winform小程序-随机抽奖软件的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: C51- NRF24L01 无线串口模块
 - 下一篇: QQ等级图标排名说明_QQ等级表,QQ最