最近班上写了一个关于winform的实训小项目(抽奖系统),90%的代码都在下面,感觉还不错,所以贴出来分享一下,希望能帮倒大家

所有界面如下:

这是自己写的一个公共类:

View Code

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace WindowsApplication1
{class NumberManage{public static int K = 1;//自保留参数,用来实现特殊功能//幸运奖号码文件保存位置private static string lukyNum = "c:\\luky.txt";        public static string LUKYNUM {get { return NumberManage.lukyNum; }           }//一,二,三等奖号码文件保存位置private static string wondofulNum = "c:\\wondful.txt";public static string WONDOFULNum{get { return NumberManage.wondofulNum; }set { NumberManage.wondofulNum = value; }}/// <summary>/// 输出一个三位数的数字,不足三位前面补零/// </summary>/// <param name="a"></param>/// <returns></returns>public static string FormatNum(int a){if (a >= 1000 || a < 0){return "000";}if (a >= 100){return a.ToString();}else if (a >= 10 && a < 100){return "0" + a;}else{return "00" + a;}}/// <summary>/// 在指定范围内随机获得一个不重复的幸运数,此数将会被保存在lukyNum路径处/// </summary>/// <returns></returns>public static int GetLukyNumber(){string numstr;//此变量用来存要写入文件中的值if (!File.Exists(lukyNum)){//创建一个文本文件之后马上关闭流
                File.CreateText(lukyNum).Close();}StreamReader sr = new StreamReader(lukyNum);string ln = sr.ReadToEnd(); sr.Close();Random r = new Random();int temp = r.Next(1, 481);if (ln == ""){numstr = temp.ToString();}else{string[] num = ln.Split(',');//当文件中已经存在480个不同的数时,则返回-1,表示无数可取if (num.Length > 480)return 0;List<string> list = new List<string>(num);
//利用List的Contains方法判断一个值是否存在,不知道数组是否有类似的方法?while (list.Contains(temp.ToString())){temp = r.Next(1, 481);}numstr = "," + temp;//非文件中第一个数时,要用","隔开
            }StreamWriter sw = new StreamWriter(lukyNum, true);sw.Write(numstr);sw.Close();return temp;}/// <summary>/// 在指定范围内随机获得一个不重复的数,此数将会被保存在wondofulNum路径处,这是用来获得一,二,三等奖的号码/// </summary>/// <returns></returns>public static int GetWondfulNumber(int level){string numstr;//此变量用来存要写入文件中的值           if (!File.Exists(wondofulNum)){//创建一个文本文件之后马上关闭流
                File.CreateText(wondofulNum).Close();}StreamReader sr = new StreamReader(wondofulNum);string ln = sr.ReadToEnd();sr.Close();Random r = new Random();int temp = r.Next(1, 481);if (ln == ""){numstr = temp.ToString();}else{string[] num = ln.Split(',');//当文件中已经存在480个不同的数时,则返回-1,表示无数可取if (num.Length > 480)return 0;List<string> list = new List<string>(num);//利用List的Contains方法判断一个值是否存在while (list.Contains(temp.ToString())){temp = r.Next(1, 481);}numstr = "," + temp;//非文件中第一个数时,要用","隔开
       }StreamWriter sw = new StreamWriter(wondofulNum, true);sw.Write(numstr);sw.Close();return temp;}/// <summary>/// 分解数字, 获得num这个数wei位上的数字,暂时简单取一个数的个位,十位和百位,其他以后完善/// </summary>/// <param name="num">要操作的数</param>/// <param name="wei">要取的位数</param>/// <returns></returns>public static int AnalyzeNum(int num, int wei){switch (wei){case 1://个位break;                   case 2://十位num = num / 10;break;case 3://百位num = num / 100;break;}return num % 10;}}
}

大图片的后台代码(用户控件):

View Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{public partial class ScrollNumBig : UserControl{private int num=0;//最后显示的数private int degree;//定义滚动次数private int maxImgIndex = 9;//要显示的数字最大值(0-maxImgIndex)private int step;//滚动的步长public int MaxImgIndex{set { maxImgIndex = value; }}public int Num{set { num = value; }}public ScrollNumBig(){step = new Random().Next(30, 50);//步长为随机数
            InitializeComponent();this.pbNumber.Image = imgNumber.Images[0];//设置图片框的初始图片为0
        }private void timer1_Tick(object sender, EventArgs e){pbNumber.Location =new Point(pbNumber.Location.X,pbNumber.Location.Y + step) ;//每次移动stepif (pbNumber.Location.Y > this.Size.Height)//如果图片全部移出界面,则把图片放到最上面
            {pbNumber.Location = new Point(pbNumber.Location.X, -pbNumber.Size.Height);degree--;//减少滚动次数if (degree < 7){timer1.Interval += 5;//最后6圈开始减速
                }if (degree > 1){//换图Random r = new Random();int imgIndex = r.Next(0, maxImgIndex+1);this.pbNumber.Image = imgNumber.Images[imgIndex];}else{this.pbNumber.Image=imgNumber.Images[num];//最后一次显示实际数字timer1.Stop();//停止timer1,timer2开始接管跑最后一圈
                   timer2.Start();timer2.Interval = timer1.Interval;}}           }/// <summary>/// 图片开始滚动,自动停止/// </summary>public void Start(){Random r = new Random();degree = new Random().Next(50,60);//设置滚动次数(50-60)之间
            timer1.Start();}public void Stop(){}private void timer2_Tick(object sender, EventArgs e){if (pbNumber.Location.Y > 0){pbNumber.Location = new Point(pbNumber.Location.X, 1);timer2.Stop();}else{pbNumber.Location = new Point(pbNumber.Location.X, pbNumber.Location.Y + step-8);timer2.Interval += 5;}}}
}

小图片的后台代码(用户控件):

View Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{public partial class ScrollNumSmall : UserControl{private int num=0;//最后显示的数private int degree;//定义滚动次数private int maxImgIndex = 9;//要显示的数字最大值(0-maxImgIndex)private int step;//滚动的步长public int MaxImgIndex{set { maxImgIndex = value; }}public int Num{set { num = value; }}public ScrollNumSmall(){step = new Random().Next(40, 50);//步长为随机数,让图片滚动不同步
            InitializeComponent();this.pbNumber.Image = imgNumber.Images[0];//设置图片框的初始图片为0
        }private void timer1_Tick(object sender, EventArgs e){pbNumber.Location =new Point(pbNumber.Location.X,pbNumber.Location.Y + step) ;//每次移动20if (pbNumber.Location.Y > this.Size.Height)//如果移到控件处,则把图片放到最上面
            {pbNumber.Location = new Point(pbNumber.Location.X, -pbNumber.Size.Height);degree--;//减少滚动次数if (degree < 7){timer1.Interval += 5;//最后6圈开始减速,移动时间每次多加6ms
                }if (degree > 1){//换图Random r = new Random();int imgIndex = r.Next(0, maxImgIndex+1);this.pbNumber.Image = imgNumber.Images[imgIndex];}else{this.pbNumber.Image=imgNumber.Images[num];//最后一次显示实际数字timer1.Stop();//停止timer1,timer2开始接管跑最后一圈
                   timer2.Start();timer2.Interval = timer1.Interval;}}          }/// <summary>/// 图片开始滚动,自动停止/// </summary>public void Start(){Random r = new Random();degree = new Random().Next(50,60);//设置滚动次数
            timer1.Start();}private void timer2_Tick(object sender, EventArgs e){if (pbNumber.Location.Y > 0){pbNumber.Location = new Point(pbNumber.Location.X, 1);timer2.Stop();}else{pbNumber.Location = new Point(pbNumber.Location.X, pbNumber.Location.Y + step-8);timer2.Interval += 5;}}}
}

幸运奖的后台代码(用户控件):

View Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{/// <summary>/// 变换的数字/// </summary>public partial class JupNumber : UserControl{private int num = 0;//定义最终的数字,默认为0private int degree = 80;public int Degree{set { degree = value; }}public int Num{set { num = value; }}private string headNum = "53500";//数字前面固定部位public JupNumber(){degree = new Random().Next(80, 100);InitializeComponent();}/// <summary>/// 开始跳动/// </summary>public void Start(){timer1.Stop();timer1.Start();this.Enabled = false;}/// <summary>/// 停止跳动,将显示数字定位在最终数字上/// </summary>public void Stop(){timer1.Stop();this.lblNumber.Text = headNum + NumberManage.FormatNum(num);this.Enabled = true;}private void timer1_Tick(object sender, EventArgs e){Random r = new Random();lblNumber.Text = headNum + NumberManage.FormatNum(r.Next(1, 481));degree--;if (degree < 0){this.Stop();}}private void lblNumber_Click(object sender, EventArgs e){if (MessageBox.Show("重摇此数?", "重置提示", MessageBoxButtons.YesNo) == DialogResult.Yes){this.num = NumberManage.GetLukyNumber();this.Degree = 70;this.Start();}}}
}

一等奖窗体的后台代码:

View Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{public partial class FirstAwardForm : Form{public FirstAwardForm(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){GoGoGo();}/// <summary>/// 开始摇奖/// </summary>private void GoGoGo(){btnStart.Enabled = false;btnRestar.Enabled = true;int lukyNum = NumberManage.GetWondfulNumber(1);//得到一个幸运数//把幸运数分解出个位,十位和百位int one = NumberManage.AnalyzeNum(lukyNum, 1);//个位int two = NumberManage.AnalyzeNum(lukyNum, 2);//十位int three = NumberManage.AnalyzeNum(lukyNum, 3);//百位
snFirstNum.Num = one;snSecondNum.Num = two;snThirdNum.Num = three;snThirdNum.MaxImgIndex = 4;//百位上的数最大为4
snFirstNum.timer1.Interval = 3;snSecondNum.timer1.Interval = 5;snThirdNum.timer1.Interval = 7;snFirstNum.Start();snSecondNum.Start();snThirdNum.Start();}private void btnRestar_Click(object sender, EventArgs e){if (MessageBox.Show("你确定放弃此号码,重新摇一次?", "重置提示", MessageBoxButtons.YesNo) == DialogResult.Yes){btnStart.Enabled = true;btnRestar.Enabled = false;}}}
}

二、三等奖后台类似,就不贴出来了,下面是幸运奖窗体的后台代码:

View Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{public partial class LukyForm : Form{private int[] lukyNum = new int[15];//15个号码public LukyForm(){InitializeComponent();//LukyNumInit();//JupNumberInit();
        }/// <summary>/// 初始化幸运数字数组/// </summary>private void LukyNumInit(){for (int i = 0; i < lukyNum.Length; i++){lukyNum[i] = NumberManage.GetLukyNumber();                }}/// <summary>/// 初始化所有JupNumber要显示的数字/// </summary>private void JupNumberInit(){int i = 0;foreach (Control jup in this.Controls){if (jup is JupNumber){((JupNumber)jup).Num = lukyNum[i];i++;}}}private void button1_Click(object sender, EventArgs e){this.button1.Enabled = false;LukyNumInit();           JupNumberInit();foreach (Control jup in this.Controls){if (jup is JupNumber){((JupNumber)jup).Start();                    }}}}
}

转载于:https://www.cnblogs.com/myindex/archive/2012/05/25/luky.html

重庆海天软件工程学院的WINFORM实训项目-抽奖系统相关推荐

  1. 重庆工商职业学院综合布线实训室顺利竣工

    2020年12月30日,重庆工商职业学院综合布线实训室顺利竣工,已经移交给学校,正式投入使用.满足了学校教学实训的急需. 此项目售前阶段,唯众多次拜访学校和代理公司,利用唯众公司技术上的绝对优势和配套 ...

  2. hadoop实训报告总结及体会_管理工程学院举办电子商务实训表彰大会

    管理工程学院举办电子商务实训 表彰大会 管理工程学院举办电子商务实训 表彰大会 时间:2019-1-9      信息来源:管理工程学院 2019年1月9日下午三点,管理工程学院在8号楼创业大讲堂隆重 ...

  3. 广西科技大学鹿山学院计算机,广西科技大学鹿山学院--土木工程VR实训中心

    原标题: 广西科技大学鹿山学院--土木工程VR实训中心 一.项目概述 广西科技大学鹿山学院土木工程 VR实训基地中心(以下简称"中心")主要是对该校土木工程系的土木工程专业进行设计 ...

  4. c语言编写加油站课设题目,城市学院c语言实训题目求答案.doc

    城市学院c语言实训题目求答案 <C程序设计>题目 任务训练: 剪刀.石头.布游戏 [掌握编程的基本思路与方法.分析数据.算法(操作.控制)] 模拟该游戏的进行.要求有提示信息表征游戏进行和 ...

  5. 电子工程管理案例_管理工程学院举办电子商务实训表彰大会

    管理工程学院举办电子商务实训 表彰大会 管理工程学院举办电子商务实训 表彰大会 时间:2019-1-9      信息来源:管理工程学院 2019年1月9日下午三点,管理工程学院在8号楼创业大讲堂隆重 ...

  6. 湖北文理学院工程能力实训开班!

    为深化校企合作,产教融合助力新工科建设,提升学生工程实践能力,电巢工程能力实训班按照不同岗位类别,匹配对应的企业岗位任职能力要求对学生开展分级培养,以产业需求为导向,培养创新型.应用型人才. 7月3日 ...

  7. 中国探月计算机考试时间,关于选拔2020年波兰罗兹大嫦娥三号登月时间 学暑期实习实训项目学员的通知...

    具体如下: 一. 2020 年暑期学习项目简介 1. 项目名额:学生自主申报,如不遵照学院统一安排和临时退出,自费 10 名, 2. 项目费用: 900 欧元 / 人. 将严格遵循学院行程安排, 6. ...

  8. 实训项目2Linux基本命令,实训项目2 Linux文件系统管理

    实训项目2 Linux文件系统管理 发布时间:2011-06-24 15:51:10来源:红联作者:397778823 一. 实训目的: 1. 掌握磁盘分区的方法: 2. 掌握文件系统的建立.挂装.解 ...

  9. python实训项目-黑马程序员上海校区Python21期Django项目实训

    黑马程序员上海中心 月薪一万只是起点 关注 晚上十点,一名名Python学生正在酣畅淋漓地撸代码,手指不间断地敲击着键盘,发出机械而清脆的声音. 各个小组在经过为期4天的django项目小组开发,终于 ...

最新文章

  1. linux课程教学设计,《LINUX操作系统》课程整体教学设计.doc
  2. 自己实现苹果安装app动画
  3. Java程序-进程中的进程
  4. 【转】如何在命令行脚本中启动带参数的Windows服务
  5. 通过密钥 SFTP(三)指定不限定根目录
  6. 智能优化算法:果蝇优化算法-附代码
  7. C语言 循环群,数学函数符号
  8. mysql查询字段为null的方法
  9. java分布式开发流程,面试建议
  10. (BFS/状态压缩)HDU 5025 Saving Tang Monk
  11. 关于iOS14 访问相册权限问题
  12. iio Engine 1
  13. BigDecimal四舍五入保留两位小数
  14. 【JavaScript】实现移动小精灵
  15. matlab实现图形几何变换如平移,matlab实现平面图形的几何变换
  16. Beginning Auto Layout Tutorial in iOS 7: Part 1
  17. effective morden c++ 3
  18. 倍斯特快人一步 执着快充移动电源
  19. [IOS APP]曾仕强演说精选-有声系列
  20. SAP ERP SD模块中维护销售人员

热门文章

  1. 软件测试修炼之道-转载
  2. 录音转文字怎么弄?分享三个快速录音转文字的方法
  3. 报表开发工具FastReport.NET的十大常见问题及解决方法
  4. wps mysql ubuntu_Ubuntu系统安装wps-office
  5. 1-3NF,BCNF,最小依赖集,模式分解,判断是否为无损分解
  6. 传智168期 SSH网上商城笔记day41~day45(2017年4月15日15:57:30)
  7. 深入理解D3D12资源状态转换
  8. CAD软件绘图如何提高效率 (上)
  9. nvidia官方cuda性能测试软件,NVIDIA CUDA应用软件测试
  10. 马云现身西班牙度假 半年多来首次在境外露面