基于C#怎么设计一个双色球选号工具
更新:HHH   时间:2023-1-7


这篇文章主要介绍了基于C#怎么设计一个双色球选号工具的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇基于C#怎么设计一个双色球选号工具文章都会有所收获,下面我们一起来看看吧。

窗体展示

开始后展示

结束摇色展示

代码

导入的命名空间

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

初始化私有变量

private string[] red = { "01","02","03","04","05","06",
        "07","08","09","10","11","12","13","14","15","16","17"};
        private string[] blue = {"01","02","03","04","05","06",
        "07","08","09","10" };
        private bool begin = true;

页面初始化

private void Form1_Load(object sender, EventArgs e)
        {
            
            foreach(var item in groupBox1.Controls)
            {
                if(item is Label)
                {
                    ((Label)item).Text = "00";
                 
                }
            }
            button2.Enabled = false;

        }

开始按钮

private void button1_Click(object sender, EventArgs e)
        {
            button1.Text = "已经开始";
            button1.Enabled = false;
            button2.Enabled = true;
            begin = true;

            TaskFactory f = new TaskFactory();
            List<Task> list = new List<Task>();
            foreach(var item in groupBox1.Controls)
            {
                if(item is Label)
                {
                    ((Label)item).Text = "00";
                }
            }
            foreach( var item in groupBox1.Controls)
            {
                if(item is Label)
                {
                    Label lbl = item as Label;
                    list.Add(f.StartNew(() =>
                    {
                        Random r = new Random();
                        while (begin)
                        {
                            string txt = "";
                            if (lbl.Name.Contains("Red"))
                            {
                                lock (obj)
                                {
                                    
                                    txt = red[r.Next(0, 17)];
                                    Update(lbl, txt);
                                    
                                }
                            }
                            else
                            {
                                txt = blue[r.Next(0, 10)];
                                Update(lbl, txt);
                            }
                            Thread.Sleep(10);
                        }
                    }));
                }
            }
            f.ContinueWhenAll(list.ToArray(), tList => this.ShowResult());
        }

数字更新

private void Update(Label label , string text)
        {
            this.Invoke(new Action(() =>
            {
                label.Text = text;
            }));
        }

结束按钮

private void button2_Click(object sender, EventArgs e)
        {
            button1.Text = "开始";
            button1.Enabled = true;
            button2.Enabled = false;
            begin = false;
        }

结果显示

private void ShowResult()
        {
            StringBuilder s = new StringBuilder();
            s.Append(lblRed1.Text + " ");
            s.Append(lblRed2.Text + " ");
            s.Append(lblRed3.Text + " ");
            s.Append(lblRed4.Text + " ");
            s.Append(lblRed5.Text + " ");
            s.Append(lblRed6.Text + " \n");
            s.Append($"blueBob: {lblBlue.Text}");
            MessageBox.Show(s.ToString());
        }

关于“基于C#怎么设计一个双色球选号工具”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“基于C#怎么设计一个双色球选号工具”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注天达云行业资讯频道。

返回开发技术教程...