有一道题完全不会,求大神帮助,明天就考试了T T
1.使用winform编程完成一个随机数生成器。2.要求用户从编辑框输入生成随机数的时间间隔
3.程序按照用户的输入,每时间间隔生成一个1-100之间的随机数,在界面中显示。
4.要求程序设置开始按钮与停止按钮,点击开始按钮开始生成,点击停止按钮不再生成新的随机数
程序代码:using System;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private System.Threading.Timer timer;
private Random random;
private SynchronizationContext context;
public Form1()
{
InitializeComponent();
random = new Random();
timer = new System.Threading.Timer(CreadeRandom, null, Timeout.Infinite, Timeout.Infinite);
context = SynchronizationContext.Current;
}
private void CreadeRandom(object obj)
{
context.Post((t) => { listBox1.Items.Add(random.Next(1, 101)); }, obj);
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox1.Items.Add("随机数生成开始!");
timer.Change(0, int.Parse(textBox1.Text));
}
private void button2_Click(object sender, EventArgs e)
{
timer.Change(Timeout.Infinite, Timeout.Infinite);
listBox1.Items.Add("随机数生成结束!");
}
}
}
程序代码:namespace WindowsFormsApplication1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private 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.components = new ();
this.serialPort1 = new (this.components);
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(22, 29);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(137, 12);
this.label1.TabIndex = 0;
this.label1.Text = "生成时间间隔(毫秒):";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(165, 26);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "1000";
//
// button1
//
this.button1.Location = new System.Drawing.Point(291, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "开始";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(372, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "停止";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(39, 80);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(475, 340);
this.listBox1.TabIndex = 4;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(550, 453);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private serialPort1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ListBox listBox1;
}
}