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

开始画面

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

抽奖中:

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

抽奖结果:

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

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

  public  class RandomHelper
{ public int GetNum(int min, int max)
{
Thread.Sleep();//随机休息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 = ; i < guid.Length; i++)
{
switch (guid[i])
{
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
seed = seed + ;
break;
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
seed = seed + ;
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 + ;
break;
default:
seed = seed + ;
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 = {
"","","","","","","","","","","","","",
"","","","","","","","","","","","","",
"","","","","","",""
};
private string[] BuleNum = {
"","","","","","","","","","","","","",
"","",""
}; 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(,);
string blueText = BuleNum[num];
this.UpdateLbl(lbl, blueText);
}
else
{
int num = randomHelper.GetNum(, );
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(""))
{
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));
}
}
上一篇:gensurf


下一篇:VBA表格单元格替换文字