实现后的效果图:
步骤:
1.创建Windows窗体应用程序项目
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
namespace LuckyDraw
{
public partial class Form1 : Form
{
public static ArrayList data;
public Form1()
{
InitializeComponent();
data = new ArrayList();
}
private void ClearDataBtn_Click(object sender, EventArgs e)
{
data.Clear();
Form_DataLabel.Text = "";
}
private void StartBtn_Click(object sender, EventArgs e)
{
int num = 0;
string allData = "";
int oneText = int.Parse(textBox1.Text);
int twoText = int.Parse(textBox2.Text);
Random ro = new Random();
long tick = DateTime.Now.Ticks;
Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
num = ro.Next(oneText, twoText+1);
if (data.Count == 0)
{
//首数据直接添加
data.Add(num);
for (var j = 0; j < data.Count; j++)
{
allData += " " + data[j];
}
Form_DataLabel.Text = allData;
}
else
{
DG(num, ro, data, false, oneText, twoText);
}
}
//递归
public void DG(int num, Random ro, ArrayList data, bool state,int oneText,int twoText)
{
string allData = "";
int length = data.Count;
try
{
if (state == false)
{
for (var i = 0; i < length; i++)
{
if (int.Parse(data[i].ToString()) == num)
{
//若存在
num = ro.Next(oneText, twoText + 1);
DG(num, ro, data, false, oneText, twoText);
break;
}
else if ((i + 1) == length)
{
//若不存在才添加
data.Add(num);
for (var j = 0; j < length; j++)
{
allData += " " + data[j];
}
Form_DataLabel.Text = allData;
}
}
}
}
catch {
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
Form1.Designer.cs:
namespace LuckyDraw
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.StartBtn = new System.Windows.Forms.Button();
this.ClearDataBtn = new System.Windows.Forms.Button();
this.Form_DataLabel = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// StartBtn
//
this.StartBtn.Location = new System.Drawing.Point(50, 89);
this.StartBtn.Name = "StartBtn";
this.StartBtn.Size = new System.Drawing.Size(75, 23);
this.StartBtn.TabIndex = 0;
this.StartBtn.Text = "开始抽奖";
this.StartBtn.UseVisualStyleBackColor = true;
this.StartBtn.Click += new System.EventHandler(this.StartBtn_Click);
//
// ClearDataBtn
//
this.ClearDataBtn.Location = new System.Drawing.Point(151, 89);
this.ClearDataBtn.Name = "ClearDataBtn";
this.ClearDataBtn.Size = new System.Drawing.Size(75, 23);
this.ClearDataBtn.TabIndex = 2;
this.ClearDataBtn.Text = "清空数据";
this.ClearDataBtn.UseVisualStyleBackColor = true;
this.ClearDataBtn.Click += new System.EventHandler(this.ClearDataBtn_Click);
//
// Form_DataLabel
//
this.Form_DataLabel.Location = new System.Drawing.Point(12, 37);
this.Form_DataLabel.Name = "Form_DataLabel";
this.Form_DataLabel.Size = new System.Drawing.Size(244, 37);
this.Form_DataLabel.TabIndex = 3;
this.Form_DataLabel.Text = " ";
this.Form_DataLabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(59, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(66, 22);
this.textBox1.TabIndex = 4;
this.textBox1.Text = "1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(146, 12);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(65, 22);
this.textBox2.TabIndex = 5;
this.textBox2.Text = "100";
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(131, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(9, 12);
this.label1.TabIndex = 6;
this.label1.Text = "-";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(268, 124);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.Form_DataLabel);
this.Controls.Add(this.ClearDataBtn);
this.Controls.Add(this.StartBtn);
this.Name = "Form1";
this.Text = "抽奖系统";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button StartBtn;
private System.Windows.Forms.Button ClearDataBtn;
private System.Windows.Forms.Label Form_DataLabel;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
}
}
源码下载地址: