我想通過使用下面的代碼在隨機間隔中運行計時器。問題是,當我這樣做,我只能貼1的隨機數,我不知道如何用我的代碼獲取下一個隨機數:使用隨機時間間隔計時器
using System;
using System.Windows.Forms;
namespace Auto_Typer
{
public partial class AutoTyper : Form
{
public AutoTyper()
{
InitializeComponent();
tmrInterval.Tick += new EventHandler(Interval);
txtInterval.TextChanged += new EventHandler(TextChanged);
txtMin.TextChanged += new EventHandler(TextChanged);
txtMax.TextChanged += new EventHandler(TextChanged);
}
private void TextChanged(object sender, EventArgs e)
{
if (int.Parse(txtInterval.Text) < 1 || int.Parse(txtMin.Text) < 1 || int.Parse(txtMax.Text) < 1)
{
txtInterval.Text = "1";
txtMin.Text = "1";
txtMax.Text = "1";
}
else if (int.Parse(txtInterval.Text) > 100)
{
txtInterval.Text = "100";
txtMin.Text = "100";
txtMax.Text = "100";
}
else if (int.Parse(txtMin.Text) >= int.Parse(txtMax.Text))
{
txtMax.Text = (int.Parse(txtMin.Text) + 1).ToString();
}
}
void Interval(object sender, EventArgs e)
{
SendKeys.Send(txtText.Text + ", " + tmrInterval.Interval + "{enter}");
}
private void btnStart_Click(object sender, EventArgs e)
{
if (tbType.SelectedTab == tbInterval)
{
tmrInterval.Interval = int.Parse(txtInterval.Text) * 1000;
tmrInterval.Enabled = true;
}
if (tbType.SelectedTab == tbRange)
{
Random random = new Random();
tmrInterval.Interval = (random.Next(int.Parse(txtMin.Text), int.Parse(txtMax.Text)) * 1000);
tmrInterval.Enabled = true;
}
}
private void btnStop_Click(object sender, EventArgs e)
{
tmrInterval.Enabled = false;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
爲什麼使用當前蜱作爲種子獲得一個隨機值時,你可以調用默認的構造函數,它「初始化一個新的實例Random類的使用時間依賴的默認種子值「? – phoog 2012-01-06 04:44:59