(急)c# winform 定时器设置了2秒执行一次,我想让它执行5次就自动停止,求代码
c#winform定时器设置了2秒执行一次,我想让它执行5次就自动停止,求代码项目在下面namespace定时器练习1{publicpartialclassForm1:F...
c# winform 定时器设置了2秒执行一次,我想让它执行5次就自动停止,求代码
项目在下面
namespace 定时器练习1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Load+=Form1_Load; }
private void timer1_Tick(object sender, EventArgs e) { richTextBox1.Text += "\n" + "aaa"; }
private System.Timers.Timer myTimer; private void Form1_Load(object sender, EventArgs e) { myTimer = new System.Timers.Timer(2000); myTimer.Elapsed += timer1_Tick; myTimer.AutoReset = true; }
private void button1_Click(object sender, EventArgs e) { myTimer.Enabled = true; Control.CheckForIllegalCrossThreadCalls = false; }
private void button2_Click(object sender, EventArgs e) { if (myTimer.Enabled) { myTimer.Enabled = false; button2.Text = "continue"; } else { myTimer.Enabled = true; button2.Text = "pause"; } }
private void button3_Click(object sender, EventArgs e) { myTimer.Close(); myTimer.Dispose(); richTextBox1.Text = richTextBox1.Text + "\n" + "over"; }
}} 展开
项目在下面
namespace 定时器练习1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Load+=Form1_Load; }
private void timer1_Tick(object sender, EventArgs e) { richTextBox1.Text += "\n" + "aaa"; }
private System.Timers.Timer myTimer; private void Form1_Load(object sender, EventArgs e) { myTimer = new System.Timers.Timer(2000); myTimer.Elapsed += timer1_Tick; myTimer.AutoReset = true; }
private void button1_Click(object sender, EventArgs e) { myTimer.Enabled = true; Control.CheckForIllegalCrossThreadCalls = false; }
private void button2_Click(object sender, EventArgs e) { if (myTimer.Enabled) { myTimer.Enabled = false; button2.Text = "continue"; } else { myTimer.Enabled = true; button2.Text = "pause"; } }
private void button3_Click(object sender, EventArgs e) { myTimer.Close(); myTimer.Dispose(); richTextBox1.Text = richTextBox1.Text + "\n" + "over"; }
}} 展开
展开全部
int i=0;
private void timer1_Tick(object sender, EventArgs e){
if(i<5)
{
richTextBox1.Text += "\n" + "aaa"; i++;
}
else
{
myTimer.Stop();button3_Click(sender,e);
}
}
private void button1_Click(object sender, EventArgs e)
{
myTimer.Enabled = true;
Control.CheckForIllegalCrossThreadCalls = false;
i=0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询