窗体展示
开始后展示
结束摇色展示
代码
导入的命名空间
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#双色球的资料请关注编程网其它相关文章!