C#中随机数和定时器的使用
C#中的定时器使用:
namespace SYA3 { public partial class Form1 : Form { int Flag = 0; //redio button标志 Timer tmr;
float size; public Form1() { InitializeComponent(); tmr = new Timer(); //申明定时器
size = Result.Font.Size;//保存最初字体 } private void label2_Click(object sender, EventArgs e) { } private void label3_Click(object sender, EventArgs e) { } private void Begin_Btn_Click(object sender, EventArgs e) {//由于Lable中的字体是只读的,所以在此必须重新画字体大小
Result.Font= new System.Drawing.Font("宋体", size, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); tmr.Tick += new EventHandler(tmr_Elapsed); //在此设置定时器的事件委托,即在特定的时间该需要做什么
tmr.Interval = int.Parse(Time_Edit.Text); //设置时间间隔
tmr.Start(); //让定时器开始定时处理事情 } private void End_Btn_Click(object sender, EventArgs e) { tmr.Stop(); Result.Font = new System.Drawing.Font("宋体", 2*size, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); } private void groupBox2_Enter(object sender, EventArgs e) { } private void Min_edit_TextChanged(object sender, EventArgs e) { } private void radioButton1_CheckedChanged(object sender, EventArgs e) { Flag = 0; } private void radioButton2_CheckedChanged(object sender, EventArgs e) { Flag = 1; } public void tmr_Elapsed(object sender, EventArgs e) { Random rand = new Random(); if (Flag == 0) { Result.Text = rand.Next(int.Parse(Min_edit.Text), int.Parse(Max_Edit.Text)) + ""; } else Result.Text = rand.NextDouble() + ""; } private void textBox1_TextChanged(object sender, EventArgs e) { } } }