C#Winform–动态点名系统
作者心得
- 真随机点名
- 动态点名界面
- 文件txt直接导入
- 设置快慢
- 人数自定义
-自动读取文件: F:\点名系统.txt
他真的很棒!
源代码
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.IO;
namespace DMXT
{
public partial class Form1 : Form
{
int mm = 0;
Button[] b = new Button[100];
int x = 0, y = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.BackColor = Color.FromArgb(0x7fdd2378);
if (!File.Exists("F:\\点名系统.txt") )
File.Create("F:\\点名系统.txt" ).Close();
for (int i = 0; i < 100; i++)
{
Button button = new Button();
button.Size = new Size(80, 40);
button.BackColor = Color.White;
if (i % 10 == 0 && i != 0)
{
x = 0;
y += 40;
}
else if (i % 10 != 0 && i != 0)
{
x += 80;
}
button.Location = new Point(x, y);
b[i] = button;
Controls.Add(b[i]);
}
string[] data = new string[100];
data = read(out int m);
mm = m;
for (int i = 0; i < 100; i++)
{
b[i].Text = data[i];
}
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "开始点名")
{
timer1.Enabled = true;
button1.Text = "停止点名";
button1.BackColor = Color.Red;
}
else
{
timer1.Enabled = false;
button1.Text = "开始点名";
button1.BackColor = Color.FromArgb(0x7fdd2378);
}
}
Random o = new Random();
int oo = 0;
int mid = 99;
private void timer1_Tick(object sender, EventArgs e)
{
oo = o.Next(0, mm);
b[oo].BackColor = Color.Red;
if(oo!=mid)
b[mid].BackColor = Color.White;
mid = oo;
}
private void btn_dis_Click(object sender, EventArgs e)
{
timer1.Interval += 50;
}
private void btn_add_Click(object sender, EventArgs e)
{ if(timer1.Interval>50)
timer1.Interval -= 50;
}
public string[] read(out int j)
{
j = 0;
int i = 0;
string[] data = new string[100];
StreamReader sr = new StreamReader("F:\\点名系统.txt", Encoding.Default);
while (sr.EndOfStream != true)
{
data[i] = sr.ReadLine();
i++;
}
foreach (string k in data)
{
if (k == null)
goto end;
j++;
}
end:
return data ;
}
}
}
效果截图
(效果不是重点,我也懒的再去自己编名字导入数据了)
作者的话
这是我第一次使用io流来操作文件
说实在的,c#的io流总感觉有些绕,不如python来的直接
这个也许真有点什么用