我弄了随机抽号,但算法不严谨,会出现重复,求帮助
using using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 0, j = 0, sign;
string[] a, b;
private void xiaocao()
{
string strsread;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
timer1.Stop();
StreamReader file = new StreamReader(openFileDialog1.FileName, Encoding.Default);
strsread = file.ReadLine();
if (strsread == null)
{
MessageBox.Show("请选择一个内容非空的文件");
xiaocao();
}
while (strsread != null)
{
j++;
strsread = file.ReadLine();
}
a = new string[j];
b = new string[j];
StreamReader file2 = new StreamReader(openFileDialog1.FileName, Encoding.Default);
strsread = file2.ReadLine();
while (strsread != null)
{
sign = strsread.IndexOf(":");
a[i] = strsread.Substring(0,sign);
b[i] = strsread.Substring(sign);
i++;
strsread = file2.ReadLine();
}
file.Dispose();
file2.Dispose();
}
else MessageBox.Show("请选择一个文件");
}
private void button1_Click(object sender, EventArgs e)
{
xiaocao();
}
Random ranNum = new Random();
int intNum = 0;
private void timer1_Tick(object sender, EventArgs e)
{
intNum = ranNum.Next(0, j);
label2.Text = a[intNum];
}
private void button2_Click(object sender, EventArgs e)
{
if (j > 0)
{
timer1.Start();
}
else
{
timer1.Dispose();
MessageBox.Show("已无东西可抽取");
label2.Text = "";
}
}
private void button3_Click(object sender, EventArgs e)
{
timer1.Stop();
MessageBox.Show(a[intNum] + " " + b[intNum]);
for (int k = 1; k<= j-1; k++)
{
string c = a[intNum];
a[intNum] = a[intNum-1];
a[intNum-1] = c;
}
j--;//是不是红色这里的问题呢?
}
private Size beforeResizeSize = Size.Empty;
protected override void OnResizeBegin(EventArgs e)
{
base.OnResizeBegin(e);
beforeResizeSize = this.Size;
}
protected override void OnResizeEnd(EventArgs e)
{
base.OnResizeEnd(e);
//窗口resize之后的大小
Size endResizeSize = this.Size;
//获得变化比例
float percentWidth = (float)endResizeSize.Width / beforeResizeSize.Width;
float percentHeight = (float)endResizeSize.Height / beforeResizeSize.Height;
foreach (Control control in this.Controls)
{
if (control is DataGridView)
continue;
//按比例改变控件大小
control.Width = (int)(control.Width * percentWidth);
control.Height = (int)(control.Height * percentHeight);
//为了不使控件之间覆盖 位置也要按比例变化
control.Left = (int)(control.Left * percentWidth);
control.Top = (int)(control.Top * percentHeight);
}
}
}
}