winform小程序-随机抽奖软件
下面介紹我做的一個(gè)winform程序,實(shí)現(xiàn)的結(jié)果是點(diǎn)擊開始,然后名字一個(gè)一個(gè)地跳躍,然后點(diǎn)擊介紹,名字停止跳動(dòng),此名字幸運(yùn)的得了獎(jiǎng),而且不會(huì)重復(fù)名單!我準(zhǔn)備用wpf來做的,雖然兩者有很大的同共處,但是也有很微妙的區(qū)別。我做的這個(gè)軟件在winform中需要timer控件,而在wpf中就沒有timer控件了,需要DispatcherTimer這個(gè)類,而使用這個(gè)類的時(shí)候出現(xiàn)了很多問題,所以暫時(shí)先放置在了一邊,用了winform窗體程序做了這個(gè)軟件。此軟件分了兩個(gè)窗體,一個(gè)主窗體和一個(gè)子窗體,主窗體是抽獎(jiǎng)用的,而子窗體是實(shí)現(xiàn)對(duì)名單的增刪改查功能。
點(diǎn)擊主窗體左上角的“查看人員名單”就會(huì)跳轉(zhuǎn)到子窗體。
過程很簡(jiǎn)單
1.首先給主窗體添加一些控件,加一些label,button和listbox控件。label顯示名字的跳躍,主窗體中的listbox將會(huì)獲取得到的獲獎(jiǎng)名字,然后顯示出來幾等獎(jiǎng)。“重新開始”是初始化,可以重新進(jìn)行抽獎(jiǎng)。
2.子窗體實(shí)現(xiàn)的是對(duì)名單的增刪改查,也有一個(gè)文本導(dǎo)入名單功能。
3.然后就是主要的數(shù)據(jù)庫(kù)操作了。我用的是sqlserver 2010數(shù)據(jù)庫(kù),我的想法是,這個(gè)軟件需要兩個(gè)表,一個(gè)表當(dāng)然是儲(chǔ)存名單用的,這個(gè)簡(jiǎn)單的軟件只需要id和Name兩個(gè)字段就行了。 另外一個(gè)表也有id和Name字段,初始是沒有數(shù)據(jù)的,這個(gè)表是儲(chǔ)存已經(jīng)抽到人的名單。第二張表是對(duì)比第一張表的,抽獎(jiǎng)的時(shí)候抽取第一張表中的名單,然后跟第二張表進(jìn)行比對(duì),如果有一樣的就不顯示,從而達(dá)到抽獎(jiǎng)名單不重復(fù)的目的。
string sqlStr = "select Name, NewID() as random from T_Staff where Name not in (select Name from T_Staff1) order by random ";4.上面的sql語(yǔ)言,放在timer里面,這樣timer運(yùn)行一次,此sql就運(yùn)行一次。這樣就實(shí)現(xiàn)了名字不斷跳轉(zhuǎn)的效果。
下面就把代碼貼上,其實(shí)很簡(jiǎn)單。也有很多不完善的地方,將在本文最后總結(jié)中指出!
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 = "一等獎(jiǎng)"; 33 } 34 35 private void btn2_Click(object sender, EventArgs e) 36 { 37 label1.Text = "二等獎(jiǎng)"; 38 } 39 40 private void btn3_Click(object sender, EventArgs e) 41 { 42 label1.Text = "三等獎(jiǎng)"; 43 } 44 private void btnOther_Click(object sender, EventArgs e) 45 { 46 label1.Text = "其它獎(jiǎng)項(xiàng)"; 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:/程序/抽獎(jiǎng)系統(tǒng)2/抽獎(jiǎng)系統(tǒng)2/Music/301.wav"; 53 player1.SoundLocation = "E:/程序/抽獎(jiǎng)系統(tǒng)2/抽獎(jiǎng)系統(tǒng)2/Music/3055.wav"; 54 SqlConnection conn = new SqlConnection(conStr); 55 if (label1.Text == "歡迎") 56 { 57 MessageBox.Show("請(qǐng)選擇獎(jiǎng)項(xiàng)!"); 58 } 59 else if (btnStart.Text == "開始") 60 { 61 player.PlayLooping(); 62 btnStart.Text = "結(jié)束"; 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 == "一等獎(jiǎng)") 78 { 79 listBox1.Items.Add(label2.Text); 80 } 81 else if (label1.Text == "二等獎(jiǎng)") 82 { 83 listBox2.Items.Add(label2.Text); 84 } 85 else if (label1.Text == "三等獎(jiǎng)") 86 { 87 listBox3.Items.Add(label2.Text); 88 } 89 else if (label1.Text == "其它獎(jiǎng)項(xiàng)") 90 { 91 listBox4.Items.Add(label2.Text); 92 } 93 } 94 } 95 private void 退出系統(tǒng)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("已經(jīng)初始化成功,請(qǐng)重新開始"); 120 } 121 private void 查看人員名單ToolStripMenuItem_Click(object sender, EventArgs e) 122 { 123 NameList list = new NameList(); 124 list.ShowDialog(); 125 } 126 } 主窗體后臺(tái)代碼 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 + "個(gè)員工"; 17 //List顯示數(shù)據(jù)庫(kù)中的員工名單 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("請(qǐng)?jiān)谏厦孑斎朊?/span>"); 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中的數(shù)據(jù) 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("請(qǐng)?jiān)谧筮呥x擇名字"); 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("成功導(dǎo)入"+lines.Count()+"條數(shù)據(jù)"); 107 } 108 } 子窗體代碼這個(gè)小程序的問題很多。代碼中的注釋很很少,是當(dāng)初寫的時(shí)候沒注意吧。窗體沒有美化,沒有太多時(shí)間去弄這個(gè)了。寫這種對(duì)sql操作的時(shí)候應(yīng)該把這些寫到一個(gè)類中SqlHelper,這樣直接調(diào)用這個(gè)類中的一些方法就行了,當(dāng)初沒注意,導(dǎo)致寫的時(shí)候程序語(yǔ)句很冗雜。
下面是這個(gè)程序運(yùn)行的效果
轉(zhuǎn)載于:https://www.cnblogs.com/xijianyao/archive/2013/06/09/3129400.html
總結(jié)
以上是生活随笔為你收集整理的winform小程序-随机抽奖软件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C51- NRF24L01 无线串口模块
- 下一篇: QQ等级图标排名说明_QQ等级表,QQ最