双色球随机选号器界面设计及功能实现

文章目录

    • 1 双色球随机选号器界面设计
      • 1.1 双色球随机选号器界面设计
    • 2 双色球随机选号器代码实现
      • 2.1 项目文件结构
      • 2.2 代码实现

1 双色球随机选号器界面设计

1.1 双色球随机选号器界面设计

效果如下:
双色球随机选号器界面设计及功能实现_第1张图片


2 双色球随机选号器代码实现

2.1 项目文件结构

项目文件结构如下:
双色球随机选号器界面设计及功能实现_第2张图片

2.2 代码实现

DoubleChromosphere.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xiketang.com.LotteryPro
{
    /// 
    /// 双色球类
    /// 
    /// 作用:主要是用来封装数据
    /// 
    public class DoubleChromosphere
    {
        //public string Red1Number { get; set; }
        //public string Red2Number { get; set; }
        //public string Red3Number { get; set; }
        //public string Red4Number { get; set; }
        //public string Red5Number { get; set; }
        //public string Red6Number { get; set; }
        //public string Blue7Number { get; set; }

        请大家自学完课程后,可以尝试分开保存两种球的数字
        //public List RedNumbers { get; set; }
        //public List BlueNumbers { get; set; }

        //今天我们授课:把红色球和蓝色球放到一块
        public List<string> Numbers { get; set; }
    }
}

Selector.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xiketang.com.LotteryPro
{
    /// 
    /// 选号器类
    /// 
    public class Selector
    {
        //红色球池
        public List<string> RedNumberList { get; set; }

        //蓝色球池
        public List<string> BlueNumberList { get; set; }

        //存储选择的号码
        public List<DoubleChromosphere> SelectedNums { get; set; } = new List<DoubleChromosphere>();

        private Random random = new Random();//随机数生成器(只在本类内部使用)

        /// 
        /// 构造方法:初始化球池的数字
        /// 
        public Selector()
        {
            RedNumberList = new List<string>
            {
                "01","02","03","04","05","06","07","08","09","10",
                "11","12","13","14","15","16","17","18","19","20",
                "21","22","23","24","25","26","27","28","29","30",
                "31","32","33"
            };
            BlueNumberList = new List<string>
            {
                "01","02","03","04","05","06","07","08","09","10",
                "11","12","13","14","15","16"
            };
        }
        /// 
        /// 生成7个随机不同的号码
        /// 
        /// 
        public List<string> CreateRandomNum()
        {
            List<string> numList = new List<string>();
            //生成6个红色球
            while (true)
            {
                if (numList.Count == 6) break;//如果随机选取了6个数字了,则退出循环
                string num = RedNumberList[random.Next(33)];
                if (numList.Contains(num)) continue;//如果是重复的数字,则重新随机抽取
                else
                {
                    numList.Add(num);
                }                  
            }
            //生成一个蓝色球
            numList.Add(BlueNumberList[random.Next(16)]);
            return numList;
        }

        //打印、远程保存等(我们在深入的VIP系统学习中再讲解...)
        //这些方法,都会用到我们所保存的《已选择号码》
    }
}

FrmMain.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace xiketang.com.LotteryPro
{
    public partial class FrmMain : Form
    {
        //创建选号器对象
        private Selector newSelector = new Selector();

        //每次得到的随机数
        List<string> randomNum = null;


        public FrmMain()
        {
            InitializeComponent();
            this.btnSelect.Enabled = false;

        }

        #region  拖动和关闭窗体

        private Point mouseOff;//鼠标移动位置变量
        private bool leftFlag;//标签是否为左键
        private void FrmMain_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;
            }
        }
        private void FrmMain_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }
        private void FrmMain_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        //启动选号
        private void btnStart_Click(object sender, EventArgs e)
        {
            this.timerCreateNum.Start();//启动定时器
            this.btnSelect.Enabled = true;
            this.btnStart.Enabled = false;
        }
        //定时器事件:每隔50ms调用一次
        private void timerCreateNum_Tick(object sender, EventArgs e)
        {
            //通过调用选号器,得到随机数
            randomNum = newSelector.CreateRandomNum();
            //在下面的列表中显示
            this.lblNum1.Text = randomNum[0];
            this.lblNum2.Text = randomNum[1];
            this.lblNum3.Text = randomNum[2];
            this.lblNum4.Text = randomNum[3];
            this.lblNum5.Text = randomNum[4];
            this.lblNum6.Text = randomNum[5];
            this.lblNum7.Text = randomNum[6];
        }
        //选择
        private void btnSelect_Click(object sender, EventArgs e)
        {
            this.timerCreateNum.Stop();
            this.btnSelect.Enabled = false;
            this.btnStart.Enabled = true;

            //创建双色球对象
            DoubleChromosphere dc = new DoubleChromosphere
            {
                //Numbers = new List()
                //{
                //    this.lblNum1.Text,
                //    this.lblNum2.Text,
                //    this.lblNum3.Text,
                //    this.lblNum4.Text,
                //    this.lblNum5.Text,
                //    this.lblNum6.Text,
                //    this.lblNum7.Text
                //}
                Numbers = this.randomNum  //我们因为已经保存了当前的一组数字,所以可以直接复制
            };

            //添加到选号池,以备后面的打印、远程保存等方法使用
            this.newSelector.SelectedNums.Add(dc);

            //显示号码
            string num = string.Empty;
            foreach (string item in this.randomNum)
            {
                num += item + "  ";
            }
            this.lbNumberList.Items.Add(num);
        }
        //清除
        private void btnDel_Click(object sender, EventArgs e)
        {
            this.newSelector.SelectedNums.Clear(); //从集合中清除
            this.lbNumberList.Items.Clear();//从列表中清除
        }

        //确认购买:需要保存到远程数据库或服务器(也可以通过webapi远程提交)
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //首先打印

            //远程保存
        }

    }
}


参考资料:

  1. .NET/C#工控上位机VIP系统学习班【喜科堂互联教育】

你可能感兴趣的:(所学所思所想)