winform 抽奖程序
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; using System.IO; using Skins.Common; namespace lucy { public partial class jiang : Skins.Common.MainForm { bool go = false; int a = 0; int jiangxiang = 1; int erdengjiang = 1; int sandengjiang = 1; ArrayList arr1 = new ArrayList(); public main main2 = null; public jiang(string title,main main1) { main2 = main1; InitializeComponent(); this.Text = title; this.qqLabel1.Text = title; if (title == "抽取一等奖") { jiangxiang = 1; qqPanel1.Visible = true; qqPanel1.BringToFront(); } else if (title == "抽取二等奖") { jiangxiang = 2; qqPanel2.BringToFront(); qqPanel2.Visible = true; } else if (title == "抽取三等奖") { jiangxiang = 3; qqPanel3.BringToFront(); qqPanel3.Visible = true; } qqButton1.Click += new EventHandler(qqButton1_Click); } void qqButton1_Click(object sender, EventArgs e) { if (jiangxiang == 1) { if (qqButton1.Text == "开始") { timer1.Enabled = true; if (a == 0) { Random ran = new Random(); a = ran.Next(0, main.arr.Count - 1); } go = true; qqButton1.Text = "停止"; } else { main.arr.Remove(yidengjiang.Text); go = false; main2.zhong1.Text = yidengjiang.Text; MsgBoxForm msg1 = new MsgBoxForm("一等奖中奖用户为" + yidengjiang.Text, "一等奖中奖用户", MessageBoxButtons.OK, MessageBoxIcon.None); msg1.ShowDialog(); if (msg1.DialogResult == DialogResult.OK) { this.Close(); main2.qqComboBox1.SelectIndex = 1; } } } else if (jiangxiang == 2) { if (qqButton1.Text == "开始") { timer1.Enabled = true; if (a == 0) { Random ran = new Random(); a = ran.Next(0, main.arr.Count - 1); } go = true; qqButton1.Text = "抽取第一位二等奖用户"; } else if (qqButton1.Text == "抽取第一位二等奖用户") { erdengjiang += 1; main.arr.Remove(yidengjiang1.Text); qqButton1.Text = "抽取第二位二等奖用户"; } else if (qqButton1.Text == "抽取第二位二等奖用户") { erdengjiang += 1; main.arr.Remove(yidengjiang2.Text); qqButton1.Text = "抽取第三位二等奖用户"; } else if (qqButton1.Text == "抽取第三位二等奖用户") { erdengjiang += 1; main.arr.Remove(yidengjiang3.Text); qqButton1.Text = "抽取第四位二等奖用户"; } else if (qqButton1.Text == "抽取第四位二等奖用户") { erdengjiang += 1; main.arr.Remove(yidengjiang4.Text); qqButton1.Text = "抽取第五位二等奖用户"; } else if (qqButton1.Text == "抽取第五位二等奖用户") { erdengjiang += 1; main.arr.Remove(yidengjiang5.Text); go = false; main2.zhong2.Text = yidengjiang1.Text + " " + yidengjiang2.Text + " " +yidengjiang3.Text + " " +yidengjiang4.Text + " " +yidengjiang5.Text; MsgBoxForm msg1 = new MsgBoxForm("二等奖中奖用户为" + yidengjiang1.Text + " " + yidengjiang2.Text + " " + yidengjiang3.Text + " " + yidengjiang4.Text + " " + yidengjiang5.Text, "二等奖中奖用户", MessageBoxButtons.OK, MessageBoxIcon.None); msg1.ShowDialog(); if (msg1.DialogResult == DialogResult.OK) { this.Close(); main2.qqComboBox1.SelectIndex = 2; } } } else if (jiangxiang == 3) { if (qqButton1.Text == "开始") { timer1.Enabled = true; if (a == 0) { Random ran = new Random(); a = ran.Next(0, main.arr.Count - 1); } go = true; qqButton1.Text = "抽取第一位三等奖用户"; } else { if (sandengjiang == 50) { main.arr.Remove(label50.Text); sandengjiang += 1; Thread.Sleep(500); go = false; string str = string.Empty; string str1 = string.Empty; int aa = 0; foreach (Label lab in qqPanel3.Controls) { str += lab.Text+" "; aa += 1; if (aa%5==0) { str1 += lab.Text + "\r\n\r\n"; } else { str1 += lab.Text + " "; } } main2.zhong3.Text = str1; MsgBoxForm msg1 = new MsgBoxForm("50名三等奖用户已经抽完", "三等奖中奖用户", MessageBoxButtons.OK, MessageBoxIcon.None); msg1.ShowDialog(); if (msg1.DialogResult == DialogResult.OK) { this.Close(); main2.qqComboBox1.SelectIndex = 2; StreamWriter sw = new StreamWriter("wlan_" + System.DateTime.Now.ToShortDateString() + "_抽奖结果.txt"); sw.WriteLine("一等奖用户:\r\n" + main2.zhong1.Text + "\r\n"); sw.WriteLine("二等奖用户:\r\n" + main2.zhong2.Text + "\r\n"); sw.WriteLine("三等奖用户:\r\n" + main2.zhong3.Text + "\r\n"); sw.WriteLine(System.DateTime.Now); sw.Close(); } } else { //if (sandengjiang == 1) //{ // foreach (Label lab in qqPanel3.Controls) // { // if (lab.Name == "label" + (sandengjiang).ToString()) // { // main.arr.Remove(lab.Text); // sandengjiang += 1; // } // } //} //else //{ foreach (Label lab in qqPanel3.Controls) { if (lab.Name == "label" + (sandengjiang).ToString()) { main.arr.Remove(lab.Text); sandengjiang += 1; break; } } } qqButton1.Text = "抽取第" + ConvertInt(sandengjiang.ToString()) + "位三等奖用户"; //} } } } private void start() { if (go) { for (int i = a; i < main.arr.Count; i++) { a = i + 1; yidengjiang.Text = main.arr[i].ToString(); if (i == main.arr.Count - 1) { a = 0; } break; } } } private void start1() { if (go) { for (int i = a; i < main.arr.Count; i++) { a = i + 1; if (erdengjiang == 1) { yidengjiang1.Text = main.arr[i].ToString(); } if (erdengjiang == 2) { yidengjiang2.Text = main.arr[i].ToString(); } if (erdengjiang == 3) { yidengjiang3.Text = main.arr[i].ToString(); } if (erdengjiang == 4) { yidengjiang4.Text = main.arr[i].ToString(); } if (erdengjiang == 5) { yidengjiang5.Text = main.arr[i].ToString(); } if (i == main.arr.Count - 1) { a = 0; } break; } } } private void start2() { if (go) { for (int i = a; i < main.arr.Count; i++) { a = i + 1; foreach(Label lab in qqPanel3.Controls) { if (lab.Name == "label" + sandengjiang.ToString()) { lab.Text = main.arr[i].ToString(); while(lab.Text == "") { lab.Text = main.arr[i].ToString(); } } } if (i == main.arr.Count - 1) { a = 0; } break; } } } private void timer1_Tick(object sender, EventArgs e) { if (jiangxiang == 1) { start(); } else if (jiangxiang == 2) { start1(); } else if (jiangxiang == 3) { start2(); } } private void jiang_Load(object sender, EventArgs e) { minge.Text = "一共有" + main.arr.Count + "个待抽奖用户"; } #region 阿拉伯数字转汉文 private static string[] cstr = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; private static string[] wstr = { "零", "十", "二", "三", "四", "五", "六", "七", "八", "九" }; /// <summary> /// 阿拉伯数字转汉文 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string ConvertInt(string str) { int len = str.Length; int i; string tmpstr, rstr; rstr = ""; if (len == 1) { tmpstr = str.Substring(0, 1); rstr += string.Concat(cstr[Int32.Parse(tmpstr)]); } else { for (i = 0; i < len; i++) { tmpstr = str.Substring(i, 1); rstr += string.Concat(wstr[Int32.Parse(tmpstr)]); } } return rstr; } #endregion private void qqPanel3_Paint(object sender, PaintEventArgs e) { } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// 打开新窗体抽奖
2 void qqButton1_Click(object sender, EventArgs e)
3 {
4 if (zhong1.Text != "" && qqComboBox1.SelectIndex == 0)
5 {
6 MsgBoxForm msg1 = new MsgBoxForm("一等奖用户已经抽取请抽取其他奖项", "", MessageBoxButtons.OK, MessageBoxIcon.None);
7 msg1.ShowDialog();
8 }
9 else if (zhong2.Text != "" && qqComboBox1.SelectIndex == 1)
10 {
11 MsgBoxForm msg1 = new MsgBoxForm("二等奖用户已经抽取请抽取其他奖项", "", MessageBoxButtons.OK, MessageBoxIcon.None);
12 msg1.ShowDialog();
13 }
14 else if (zhong3.Text != "" && qqComboBox1.SelectIndex == 2)
15 {
16 MsgBoxForm msg1 = new MsgBoxForm("二等奖用户已经抽取请抽取其他奖项", "", MessageBoxButtons.OK, MessageBoxIcon.None);
17 msg1.ShowDialog();
18 }
19 else
20 {
21 jiang jiang1 = new jiang(qqComboBox1.Texts, this);
22 jiang1.ShowDialog();
23 }
24 }
25 ///载入的时候新开一个线程读取抽奖列表
26 private void main_Load(object sender, EventArgs e)
27 {
28 new Thread(() =>
29 {
30 try
31 {
32 string[] filearr = File.ReadAllLines("jiang.txt", Encoding.UTF8);
33 for (int j = 0; j < filearr.Length; j++)
34 {
35 Invoke(new Action(() =>
36 {
37 arr.Add(filearr[j].ToString());
38 }));
39 }
40 BeginInvoke(new Action(() =>
41 {
42
43 }));
44 }
45 catch (Exception ex)
46 {
47 string aa = ex.ToString();
48 }
49 }).Start();
50 }
51 ///保存中奖名单
52 SaveFileDialog saveFileDialog = new SaveFileDialog();
53 private void qqButton2_Click(object sender, EventArgs e)
54 {
55 if (zhong1.Text != "" && zhong2.Text != "" && zhong3.Text != "")
56 {
57 //saveFileDialog.InitialDirectory = "C:\\";//设置保存的默认目录
58 saveFileDialog.Filter = "txt files(*.txt)|*.txt|all files(*.*)|*.*";
59 saveFileDialog.FileName = "wlan_"+System.DateTime.Now.ToShortDateString()+"_抽奖结果";
60 saveFileDialog.FilterIndex = 1;//默认显示保存内型为TXT
61 saveFileDialog.RestoreDirectory = true;
62 if (saveFileDialog.ShowDialog() == DialogResult.OK)
63 {
64 try
65 {
66 StreamWriter sw = new StreamWriter(saveFileDialog.FileName);
67 sw.WriteLine("一等奖用户:\r\n" + zhong1.Text + "\r\n");
68 sw.WriteLine("二等奖用户:\r\n" + zhong2.Text + "\r\n");
69 sw.WriteLine("三等奖用户:\r\n" + zhong3.Text + "\r\n");
70 sw.WriteLine(System.DateTime.Now);
71 sw.Close();
72 }
73 catch (Exception ex)
74 {
75 Console.WriteLine("Err: " + ex.Message);
76 }
77 finally
78 {
79 Console.WriteLine("Write END");
80 }
81 }
82 }
83 else
84 {
85 MsgBoxForm msg1 = new MsgBoxForm("请抽奖后再导出结果", "", MessageBoxButtons.OK, MessageBoxIcon.None);
86 msg1.ShowDialog();
87
88 }
89 }
完整代码
main.cs
1 using System;
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Threading;
10 using System.IO;
11 using Skins.Common;
12
13 namespace lucy
14 {
15 public partial class main : Skins.Common.MainForm
16 {
17 public static ArrayList arr = new ArrayList();
18 public main()
19 {
20 InitializeComponent();
21 this.qqButton1.Click += new EventHandler(qqButton1_Click);
22 }
23
24 void qqButton1_Click(object sender, EventArgs e)
25 {
26 if (zhong1.Text != "" && qqComboBox1.SelectIndex == 0)
27 {
28 MsgBoxForm msg1 = new MsgBoxForm("一等奖用户已经抽取请抽取其他奖项", "", MessageBoxButtons.OK, MessageBoxIcon.None);
29 msg1.ShowDialog();
30 }
31 else if (zhong2.Text != "" && qqComboBox1.SelectIndex == 1)
32 {
33 MsgBoxForm msg1 = new MsgBoxForm("二等奖用户已经抽取请抽取其他奖项", "", MessageBoxButtons.OK, MessageBoxIcon.None);
34 msg1.ShowDialog();
35 }
36 else if (zhong3.Text != "" && qqComboBox1.SelectIndex == 2)
37 {
38 MsgBoxForm msg1 = new MsgBoxForm("二等奖用户已经抽取请抽取其他奖项", "", MessageBoxButtons.OK, MessageBoxIcon.None);
39 msg1.ShowDialog();
40 }
41 else
42 {
43 jiang jiang1 = new jiang(qqComboBox1.Texts, this);
44 jiang1.ShowDialog();
45 }
46 }
47
48 private void main_Load(object sender, EventArgs e)
49 {
50 new Thread(() =>
51 {
52 try
53 {
54 string[] filearr = File.ReadAllLines("jiang.txt", Encoding.UTF8);
55 for (int j = 0; j < filearr.Length; j++)
56 {
57 Invoke(new Action(() =>
58 {
59 arr.Add(filearr[j].ToString());
60 }));
61 }
62 BeginInvoke(new Action(() =>
63 {
64
65 }));
66 }
67 catch (Exception ex)
68 {
69 string aa = ex.ToString();
70 }
71 }).Start();
72 }
73 SaveFileDialog saveFileDialog = new SaveFileDialog();
74 private void qqButton2_Click(object sender, EventArgs e)
75 {
76 if (zhong1.Text != "" && zhong2.Text != "" && zhong3.Text != "")
77 {
78 //saveFileDialog.InitialDirectory = "C:\\";//设置保存的默认目录
79 saveFileDialog.Filter = "txt files(*.txt)|*.txt|all files(*.*)|*.*";
80 saveFileDialog.FileName = "wlan_"+System.DateTime.Now.ToShortDateString()+"_抽奖结果";
81 saveFileDialog.FilterIndex = 1;//默认显示保存内型为TXT
82 saveFileDialog.RestoreDirectory = true;
83 if (saveFileDialog.ShowDialog() == DialogResult.OK)
84 {
85 try
86 {
87 StreamWriter sw = new StreamWriter(saveFileDialog.FileName);
88 sw.WriteLine("一等奖用户:\r\n" + zhong1.Text + "\r\n");
89 sw.WriteLine("二等奖用户:\r\n" + zhong2.Text + "\r\n");
90 sw.WriteLine("三等奖用户:\r\n" + zhong3.Text + "\r\n");
91 sw.WriteLine(System.DateTime.Now);
92 sw.Close();
93 }
94 catch (Exception ex)
95 {
96 Console.WriteLine("Err: " + ex.Message);
97 }
98 finally
99 {
100 Console.WriteLine("Write END");
101 }
102 }
103 }
104 else
105 {
106 MsgBoxForm msg1 = new MsgBoxForm("请抽奖后再导出结果", "", MessageBoxButtons.OK, MessageBoxIcon.None);
107 msg1.ShowDialog();
108
109 }
110 }
111
112 private void button1_Click(object sender, EventArgs e)
113 {
114 if (zhong1.Text != "" && zhong2.Text != "" && zhong3.Text != "")
115 {
116 //saveFileDialog.InitialDirectory = "C:\\";//设置保存的默认目录
117 saveFileDialog.Filter = "txt files(*.txt)|*.txt|all files(*.*)|*.*";
118 saveFileDialog.FileName = "wlan_" + System.DateTime.Now.ToShortDateString() + "_抽奖结果判断重复";
119 saveFileDialog.FilterIndex = 1;//默认显示保存内型为TXT
120 saveFileDialog.RestoreDirectory = true;
121 if (saveFileDialog.ShowDialog() == DialogResult.OK)
122 {
123 try
124 {
125 StreamWriter sw = new StreamWriter(saveFileDialog.FileName);
126 sw.WriteLine(zhong1.Text);
127 sw.WriteLine(zhong2.Text.Replace(" ","\r\n"));
128 sw.WriteLine(zhong3.Text.Replace("\r\n", " ").Replace(" ", "\r\n").Replace(" ", "\r\n"));
129 sw.Close();
130 }
131 catch (Exception ex)
132 {
133 Console.WriteLine("Err: " + ex.Message);
134 }
135 finally
136 {
137 Console.WriteLine("Write END");
138 }
139 }
140 }
141 else
142 {
143 MsgBoxForm msg1 = new MsgBoxForm("请抽奖后再导出结果", "", MessageBoxButtons.OK, MessageBoxIcon.None);
144 msg1.ShowDialog();
145
146 }
147 }
148 }
149 }
jiang.cs
给移动做的一个抽奖程序,可能还有点漏洞,占时没有解决,抽奖的时候没有遇到什么问题。
现在把代码放出,没有带大的难度。