c# *双色球号码生成器

图片:
c# *双色球号码生成器
完整代码下载:点此下载

一:通过操作GDI+的不合理的解决方案
数字在不断变换的时候重绘    基本上都会让程序当掉
或许是我的程序没写好,希望达人指教

Code
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace lottery
{
public partial class Form1 : Form
{
private int[] arr = new int[7];
private Font font = new Font("Arial Black", 36);
private Brush brush;
private RectangleF rec = new RectangleF(0, 13, 100, 100);
private StringFormat sf = new StringFormat();
private Control[] controls;
private bool flag = true;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
controls
= new Control[] { num1, num2, num3, num4, num5, num6,num7 };
for (int i = 0; i < 7; i++)
{
controls[i].Paint
+=new PaintEventHandler(num_Paint);
}
sf.Alignment
= StringAlignment.Center;
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "stop")
{
flag
= false;
}
if (button1.Text == "start")
{
button1.Text
= "stop";
while (flag)
{
start();
}
}
}
/// <summary>
/// 开始
/// </summary>
private void start()
{
MakeNums();
for (int i = 0; i < 7; i++)
{
if (i == 6)
{
brush
= new SolidBrush(Color.Blue);
}
else
{
brush
= new SolidBrush(Color.Red);
}
Graphics draw
= Graphics.FromHwnd(controls[i].Handle);
draw.Clear(Color.Black);
draw.DrawString(arr[i].ToString(), font, brush, rec, sf);
draw.Dispose();
}
}
/// <summary>
/// 制造号码
/// </summary>
private void MakeNums()
{
Random rand
= new Random();
Hashtable ht
= new Hashtable();
//产生不重复的随机数
int key = 0;
while (ht.Count <= 6)
{
int val = rand.Next(1, 33);
if (!ht.ContainsValue(val))
{ ht.Add(key, val); key
++; }
}
//得到随机数
for (int i = 0; i < 6; i++)
{
arr[i]
= (int)ht[i];
}
//冒泡排序
int temp;
for (int outer = 5; outer >= 0; outer--)
{
for (int inner = 0; inner <= outer - 1; inner++)
{
if (arr[inner] > arr[inner + 1])
{
temp
= arr[inner];
arr[inner]
= arr[inner + 1];
arr[inner
+ 1] = temp;
}
}
}
//制造蓝色球
arr[6] = rand.Next(1, 16);
}
/// <summary>
/// 重绘事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void num_Paint(object sender, PaintEventArgs e)
{
if (arr == null) { return; }
PictureBox pb
= (PictureBox)sender;
int num = Convert.ToInt32(pb.Name.Replace("num", ""))-1;
Graphics draw
= e.Graphics;
draw.Clear(Color.Black);
if (num == 6)
{
brush
= new SolidBrush(Color.Blue);
}
else
{
brush
= new SolidBrush(Color.Red);
}
draw.DrawString(arr[num].ToString(), font, brush, rec, sf);
}
}
}

二:通过Invoke方法把控件实例传入到程序中操作的更合理的方法

Code
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace lottery
{
public partial class Form1 : Form
{
private bool flag = true;
delegate void SetTextDelegate(Label label,string text);
private Label[] labels;
private Font font;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 制造号码
/// </summary>
private int[] MakeNums()
{
int[] arr = new int[7];
Random rand
= new Random();
Hashtable ht
= new Hashtable();
//产生不重复的随机数
int key = 0;
while (ht.Count <= 6)
{
int val = rand.Next(1, 33);
if (!ht.ContainsValue(val))
{ ht.Add(key, val); key
++; }
}
//得到随机数
for (int i = 0; i < 6; i++)
{
arr[i]
= (int)ht[i];
}
//冒泡排序
int temp;
for (int outer = 5; outer >= 0; outer--)
{
for (int inner = 0; inner <= outer - 1; inner++)
{
if (arr[inner] > arr[inner + 1])
{
temp
= arr[inner];
arr[inner]
= arr[inner + 1];
arr[inner
+ 1] = temp;
}
}
}
//制造蓝色球
arr[6] = rand.Next(1, 16);
return arr;
}

private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "start")
{
flag
= true;
button1.Text
= "stop";
ThreadStart TStart
= new ThreadStart(Start);
Thread thread
= new Thread(TStart);
thread.Start();
}
else
{
flag
= false;
button1.Text
= "start";
}
}
private void Start()
{
while (flag)
{ PrintNum(); Thread.Sleep(
88); }
}
private void PrintNum()
{
int[] arr = MakeNums();
for (int i = 0; i < 7; i++)
{
SetTextDelegate mydelegate
= new SetTextDelegate(SetText);
try { Invoke(mydelegate, labels[i], arr[i].ToString()); }
catch { return; }
}
}
private void SetText(Label label, string text)
{
label.Text
= text;
label.TextAlign
= ContentAlignment.MiddleCenter;
label.Font
= font;
if (label.Name == "label7")
{ label.ForeColor
= Color.Blue; }
else
{ label.ForeColor
= Color.Red; }
}

private void Form1_Load(object sender, EventArgs e)
{
labels
= new Label[] { label1, label2, label3, label4, label5, label6, label7 };
font
= new Font("Arial Black",36);
}

}
}

三:用到了timer控件和list的sort方法的  第三种解决方案

 

c# *双色球号码生成器c# *双色球号码生成器Code
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace SS
{
    
public partial class Form_SS : Form
    {
        
private Label[] cs = null;
        
public Form_SS()
        {
            InitializeComponent();
            cs 
= new Label[] { label1, label2, label3, label4, label5, label6, label7 };
        }

        
private List<int> Create()
        {
            List
<int> t = new List<int>();
            Random ran 
= new Random(unchecked((int) DateTime.Now.Ticks));
            
for (int i = 0, n = ran.Next(134); i < 6; i++)
            {
                
while (t.Exists(delegate(int item) { return item == n; })) n = ran.Next(134);
                t.Add(n);
            }
            t.Sort();
            t.Add(ran.Next(
117));
            
return t;
        }

        
private void btn_Start_Click(object sender, EventArgs e)
        {
            btn_Start.Text 
= (timer1.Enabled = !timer1.Enabled) ? "stop" : "start";
        }

        
private void timer1_Tick(object sender, EventArgs e)
        {
            List
<int> t = Create();
            
for (int i = 0; i < t.Count; i++) cs[i].Text = t[i].ToString();
        }
    }
}


四:通过程序创建窗体的更简单的代码 

c# *双色球号码生成器c# *双色球号码生成器Code
class Program{static Random r = new Random();
static void Main(string[] args){
Form f 
= new Form { Text="xxxx" };
Button b 
= new Button { Text = "start", Top=50 };
b.Click 
+= delegate(object sender, EventArgs e) { ((Button)sender).Text = ((Button)sender).Text == "stop" ? "start" : "stop"; Do(f); };
f.Controls.AddRange(
new Control[] { b,new Label{ AutoSize = true} });
f.ShowDialog();}
static void Do(Form f){
    
while (f.Controls[0].Text == "stop"){
    List
<int> ns = new List<int>();
    
while (ns.Count != 6) { ns.Add(new Func<intint>(delegate(int t) { while (ns.Contains(t = r.Next(134))); return t;})(1)); }
    ns.Sort();
    f.Controls[
1].Text = string.Format("{0} - {1} - {2} - {3} - {4} - {5} = {6}",ns[0],ns[1],ns[2],ns[3],ns[4],ns[5], r.Next(1,17));
    Application.DoEvents();}}}

 

程序编写过程中得到了  杭州的wisdomqq   上海的aha   的帮助  感激中...

上一篇:sql里的函数


下一篇:c#简单的音乐播放器,支持多种格式,可扩展性强