c#用winform开发一个简易双色球项目

Posted rookieflying

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#用winform开发一个简易双色球项目相关的知识,希望对你有一定的参考价值。

开始画面

技术图片

抽奖中:

技术图片

 

抽奖结果:

技术图片

 

需要一个随机数Random的帮助类,让随机数唯一性

  public  class RandomHelper
    {

        public int GetNum(int min, int max)
        {
            Thread.Sleep(1000);//随机休息1秒
            return GetIntNum(min,max);
        }

        /// <summary>
        /// 获取随机数,解决重复问题
        /// </summary>
        /// <param name="min">返回的随机数字包含下限</param>
        /// <param name="max">返回的随机数字不包含上限</param>
        /// <returns></returns>
        private  int GetIntNum(int min,int max)
        {
            
            Guid Guidnum = Guid.NewGuid();
            string guid =  Guidnum.ToString();
            int seed = DateTime.Now.Millisecond;
            for (int i = 0; i < guid.Length; i++)
            {
                switch (guid[i])
                {
                    case a:
                    case b:
                    case c:
                    case d:
                    case e:
                    case f:
                    case g:
                        seed = seed + 1;
                        break;
                    case h:
                    case i:
                    case j:
                    case k:
                    case l:
                    case m:
                    case n:
                        seed = seed + 2;
                        break;
                    case o:
                    case p:
                    case q:
                    case r:
                    case s:
                    case t:
                        seed = seed + seed;
                        break;
                    case u:
                    case v:
                    case w:
                    case x:
                    case y:
                    case z:
                        seed = seed + 3;
                        break;
                    default:
                        seed = seed + 4;
                        break;
                }
            }


            return new Random(seed).Next(min,max);
            
        }

 

Form的 代码编写

先要放2个

  public Form1()
        {
            InitializeComponent();
            IsBegin = true;
            this.btnEnd.Enabled = false;
        }
        /// <summary>
        /// 标识是否开始摇奖
        /// </summary>
        private static bool IsBegin = true;

        private string[] RedNum = {
            "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"
        };
        private string[] BuleNum = {
            "01","02","03","04","05","06","07","08","09","10","11","12","13",
            "14","15","16"
        };

        private void btnStart_Click(object sender, EventArgs e)
        {
            TaskFactory taskFactory = new TaskFactory();
            List<Task> taskList = new List<Task>();
            IsBegin = true;
            this.btnStart.Enabled = false;

           // Thread.Sleep(1000);
            this.btnEnd.Enabled = true;
            foreach (Control item in gBox.Controls)
            {
                if (item is Label)
                {
                    Label lbl = (Label)item;
                    taskList.Add(taskFactory.StartNew(
                     () => {
                         while (IsBegin)
                         {
                             this.UpdateNum(lbl);
                         }
                         
                     }));
                }
            }
            taskFactory.ContinueWhenAll(taskList.ToArray(),tList=>this.ShowNumber());//等所有线程操作完毕后才显示中奖号。
          
        }
        /// <summary>
        ////// </summary>
        private static readonly object Num_Lock = new object();
        
        private void UpdateNum(Label lbl)
        {
            RandomHelper randomHelper = new RandomHelper();

            if (lbl.Name.Contains("Blue"))
            {
                int num = randomHelper.GetNum(0,16);
                string blueText = BuleNum[num];
                this.UpdateLbl(lbl, blueText);
            }
            else 
            {
                int num = randomHelper.GetNum(0, 33);
                string redText = RedNum[num];
                lock (Num_Lock)
                {
                    List<string> list = this.GetLabelList();
                    if (list.Contains(redText))
                    {
                        return;
                    }
                    else
                    {
                        this.UpdateLbl(lbl,redText);
                    }
                }
            }
        }
        /// <summary>
        /// 获取当前已经抽出的双色球,防止重复
        /// </summary>
        /// <returns>所有控件的值</returns>
        private List<string> GetLabelList()
        {
            List<string> strList = new List<string>();
            foreach (Control item in gBox.Controls)
            {
                if (item is Label)
                {
                    Label label = (Label)item;
                    strList.Add(label.Text);
                }
            }
            return strList;
        }
        /// <summary>
        /// 通过主线程修改UI
        /// </summary>
        /// <param name="lbl">修改的Label</param>
        /// <param name="text">修改的值</param>
        private void UpdateLbl(Label lbl, string text)
        {
            if (lbl.InvokeRequired)
            {
                this.Invoke(new Action(() =>
                {
                    lbl.Text = text;
                    //Thread.Sleep(2000);
                    //Console.WriteLine($"当前UpdateLbl线程id{Thread.CurrentThread.ManagedThreadId}");
                }));//交给UI线程去更新
            }
            else
            {
                lbl.Text = text;
            }
        }
        /// <summary>
        /// 结束抽奖,开奖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEnd_Click(object sender, EventArgs e)
        {

            if (!this.GetLabelList().Contains("00"))
            {
                IsBegin = false;
                this.btnEnd.Enabled = false;
                this.btnStart.Enabled = true;

            }
            else
            {
                MessageBox.Show("请慢一点,稍后再试");
            }
            
           
        }

        private void ShowNumber()
        {
            MessageBox.Show(string.Format("本期双色球结果是 {0} {1} {2} {3} {4} {5}  {6}",
                  lbRed1.Text, lbRed2.Text, lbRed3.Text, lbRed4.Text, lbRed5.Text, lbRed6.Text, lbBlue.Text));
        }
    }

 

以上是关于c#用winform开发一个简易双色球项目的主要内容,如果未能解决你的问题,请参考以下文章

c#中winform是用啥语言编写?

求C#用winform写个复数计算器,要详细点的(包括键位属性怎么改等等)

WinForm | C# 弹出简易的消息提示框 (仿Android Toast消息提示)

C# WinForm开发

Winform进度条的问题,请高手指点

用C#写了个winform程序,怎样做成安装软件供及其安装使用?