c# winform 面向对象设计体育彩票选号器 - success365

时间:2024-03-01 10:54:18

用到的知识很简单:产生随机数、保存数据到文本文件、定时器的使用等。主要体现c#面向对象设计的思想。界面效果如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 体育彩票选号器
 8 {
 9     /// <summary>
10     /// 日历类
11     /// </summary>
12     class Calendar
13     {
14         public string Year { get; set; }
15         public string Month { get; set; }
16         public string Day { get; set; }
17         public string Time { get; set; }
18         public string Week { get; set; }
19     }
20 }
  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace 体育彩票选号器
  9 {
 10     /// <summary>
 11     /// 选号器类
 12     /// </summary>
 13     class PickDevice
 14     {
 15         private Random random;
 16         private Calendar myCalendar;
 17         internal Calendar MyCalendar
 18         {
 19             get 
 20             {
 21                 this.myCalendar.Year = DateTime.Now.Year.ToString();
 22                 this.myCalendar.Month = DateTime.Now.Month.ToString("00");
 23                 this.myCalendar.Day = DateTime.Now.Day.ToString("00");
 24                 this.myCalendar.Time = DateTime.Now.ToLongTimeString();
 25                 this.myCalendar.Week = WeekToChineseWeek(DateTime.Now.DayOfWeek.ToString());
 26                 return myCalendar;
 27             }
 28             //set { myCalendar = value; }
 29         }
 30         /// <summary>
 31         /// 选中的号码
 32         /// </summary>
 33         private List<string> selectedNum;
 34 
 35         public List<string> SelectedNum
 36         {
 37             get { return selectedNum; }
 38             set { selectedNum = value; }
 39         }
 40         public PickDevice()
 41         {
 42             random = new Random();
 43             myCalendar = new Calendar();
 44             selectedNum=new List<string>();
 45         }
 46         /// <summary>
 47         /// 生成10以内随机号码
 48         /// </summary>
 49         /// <returns></returns>
 50         public int[] CreateNum()
 51         {
 52             int[] num=new int[7];
 53             num[0] = random.Next(10);
 54             num[1] = random.Next(10);
 55             num[2] = random.Next(10);
 56             num[3] = random.Next(10);
 57             num[4] = random.Next(10);
 58             num[5] = random.Next(10);
 59             num[6] = random.Next(10);
 60             return num;
 61         }
 62         public void ExportToTxt(string path)
 63         {
 64             using (FileStream fs = new FileStream(path, FileMode.Create))
 65             {
 66                 using (StreamWriter sw = new StreamWriter(fs))
 67                 {
 68                     foreach(string s in SelectedNum)
 69                     {
 70                         sw.WriteLine(s);
 71                     }
 72                 }
 73             }
 74         }
 75         /// <summary>
 76         /// 将英文日期转换为中文
 77         /// </summary>
 78         /// <param name="week"></param>
 79         /// <returns></returns>
 80         public string WeekToChineseWeek(string week)
 81         {
 82             switch (week)
 83             {
 84                 case "Monday":
 85                     return "";
 86                     //break;
 87                 case "Tuesday":
 88                     return "";
 89                     //break;
 90                 case "Wednesday":
 91                     return "";
 92                     //break;
 93                 case "Thursday":
 94                     return "";
 95                     //break;
 96                 case "Friday":
 97                     return "";
 98                     //break;
 99                 case "Saturday":
100                     return "";
101                     //break;
102                 case "Sunday":
103                     return "";
104                     //break;
105                 default:
106                     return "";
107                     //break;
108             }
109         }
110     }
111 }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 体育彩票选号器
{
    public partial class FrmMain : Form
    {
        PickDevice pickDevice = new PickDevice();
        private int order = 0;//选号的组号
        public FrmMain()
        {
            InitializeComponent();
            this.btnChoose.Enabled = false;
        }
        /// <summary>
        /// 获取日历信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tmrCalendar_Tick(object sender, EventArgs e)
        {
            this.lblYear.Text = pickDevice.MyCalendar.Year;
            this.lblMonth.Text = pickDevice.MyCalendar.Month;
            this.lblDay.Text = pickDevice.MyCalendar.Day;
            this.lblTime.Text = pickDevice.MyCalendar.Time;
            this.lblWeek.Text = pickDevice.MyCalendar.Week;
        }
        /// <summary>
        /// 产生随机数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tmrRandom_Tick(object sender, EventArgs e)
        {
            int[] num = pickDevice.CreateNum();
            this.lblNum1.Text=num[0].ToString();
            this.lblNum2.Text = num[1].ToString();
            this.lblNum3.Text = num[2].ToString();
            this.lblNum4.Text = num[3].ToString();
            this.lblNum5.Text = num[4].ToString();
            this.lblNum6.Text = num[5].ToString();
            this.lblNum7.Text = num[6].ToString();
        }
        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            tmrRandom.Start();
            this.btnChoose.Enabled = !this.btnChoose.Enabled;
            this.btnStart.Enabled=!this.btnStart.Enabled;

        }
        /// <summary>
        /// 选号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChoose_Click(object sender, EventArgs e)
        {
            this.tmrRandom.Stop();
            this.btnStart.Enabled = !this.btnStart.Enabled;
            this.btnChoose.Enabled = !this.btnChoose.Enabled;
            string num = "";
            num += this.lblNum1.Text;
            num += this.lblNum2.Text;
            num += this.lblNum3.Text;
            num += this.lblNum4.Text;
            num += this.lblNum5.Text;
            num += this.lblNum6.Text;
            num += "  ";
            num += this.lblNum7.Text;
            order++;
            num = "" + order + "组:" + num;
            lstNum.Items.Add(num);
            pickDevice.SelectedNum.Add(num);
        }
        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (this.lstNum.Items.Count == 0)
            {
                MessageBox.Show("请先选号哦~~~");
                return;
            }
            string path = DateTime.Now.ToString("yyyyMMddHHmmssms")+".txt";
            pickDevice.ExportToTxt(path);
            btnClear_Click(null,null);
            MessageBox.Show("恭喜,搞定~~~");
        }
        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.lstNum.Items.Clear();
            foreach (Control c in pnlNum.Controls)
            {
                c.Text = "0";
            }
            order = 0;
            pickDevice.SelectedNum.Clear();
        }
    }
}

 代码下载:体育彩票选号器.rar