加一个定时器:按定义的时间触发定时器

实体类:封装数据

namespace NumberSelector
{
    internal class Number
    {
        public List<string> Values { get; set; }
    }
}

 

工具类:

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

namespace NumberSelector
{
    internal class Selector
    {
        public List<string> RedNumners {  get; set; }
        public List<string> BlueNumners { get; set; }

        public List<Number> selectors { get; set; }=new List<Number>();

        //Random()随机数
        private Random random = new Random();//字段,在类里面存放数据

        public Selector() {
            //红色球池
        RedNumners = new List<string>()
        {
            "01","02","03","04","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"
        };
            //蓝色球池
            BlueNumners = new List<string>()
            {
                "01","02","03","04","05","06","07","08","09",
                "10","11","12","13","14","15","16"
            };
        }
        //方法:生成7个随机数
        public List<string> selectNumbers()
        {
            List<string> Nums = new List<string>();
            while(true)
            {
                if (Nums.Count == 6) break;
                string redNums = RedNumners[random.Next(33)];
                if (Nums.Contains(redNums)) continue;
                else Nums.Add(redNums);
            }
            string blueNums = BlueNumners[random.Next(16)];
            Nums.Add(blueNums);
            return Nums;
        }

    }
}

Main类:

using NumberSelector;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormsExcel
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        private Selector selector=new Selector();
        List<string> nums = null;//字段
        public Form1()
        {
            InitializeComponent();
            this.select.Enabled = false;
        }


        #region 拖动窗口

        private static bool IsDragging = false; //用于指示当前是不是在拖拽状态
        private Point StartPoint = new Point(0, 0); //记录鼠标按下去的坐标, new是为了拿到空间, 两个0无所谓的
                                                    //记录动了多少距离,然后给窗体Location赋值,要设置Location,必须用一个Point结构体,不能直接给Location的X,Y赋值
        private Point OffsetPoint = new Point(0, 0);

        private void FormLogin_MouseDown(object sender, MouseEventArgs e)
        {
            //如果按下去的按钮不是左键就return,节省运算资源
            if (e.Button != MouseButtons.Left)
            {
                return;
            }
            //按下鼠标后,进入拖动状态:
            IsDragging = true;
            //保存刚按下时的鼠标坐标
            StartPoint.X = e.X;
            StartPoint.Y = e.Y;
        }

        private void FormLogin_MouseMove(object sender, MouseEventArgs e)
        {
            //鼠标移动时调用,检测到IsDragging为真时
            if (IsDragging == true)
            {
                //用当前坐标减去起始坐标得到偏移量Offset
                OffsetPoint.X = e.X - StartPoint.X;
                OffsetPoint.Y = e.Y - StartPoint.Y;
                //将Offset转化为屏幕坐标赋值给Location,设置Form在屏幕中的位置,如果不作PointToScreen转换,你自己看看效果就好
                Location = PointToScreen(OffsetPoint);
            }
        }

        private void FormLogin_MouseUp(object sender, MouseEventArgs e)
        {
            //左键抬起时,及时把拖动判定设置为false,否则,你也可以试试效果
            IsDragging = false;
        }

        #endregion 拖动窗口

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        //选择
        private void select_Click(object sender, EventArgs e)
        {
            this.timerCreatNums.Stop();
            this.firing.Enabled = false;



            Number number = new Number
            {
                Values = this.nums
            };
            //把值传给选号池
            this.selector.selectors.Add(number);
            //打印
            string contentNums = string.Empty;
            int i = this.selector.selectors.Count ;
                if (i < 10) { contentNums = $"序号:0{i};双色球号码:"; }
                else contentNums = $"序号:{i};双色球号码:"; 
                foreach (string item in nums)
                {
                    contentNums+= item + " ";
                } 
            this.content.Items.Add(contentNums);
            this.timerCreatNums.Start();
        }
        //定时器
        private void timerCreatNums_Tick(object sender, EventArgs e)
        {
            nums=selector.selectNumbers();
            this.red1.Text = nums[0];
            this.red2.Text = nums[1];
            this.red3.Text = nums[2];
            this.red4.Text = nums[3];
            this.red5.Text = nums[4];
            this.red6.Text = nums[5];
            this.blue1.Text = nums[6];
        }
        //启动
        private void firing_Click(object sender, EventArgs e)
        {
            this.timerCreatNums.Start();
            this.select.Enabled = true;
        }
        //内容
        private void content_Click(object sender, EventArgs e)
        {

        }
        //清除
        private void clean_Click(object sender, EventArgs e)
        {
            this.content.Items.Clear();
        }
    }
}

 

posted on 2023-05-10 16:20  阿霖找BUG  阅读(27)  评论(0编辑  收藏  举报