110,502
社区成员
发帖
与我相关
我的任务
分享
首先在窗体上有
lable1,//运行显示
button1,//开始
button2,//暂停和继续
button3//停止
窗体上还放Timer控件timer1
代码实现如下,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace testMain
{
public partial class Form1 : Form
{
public enum RunState
{
running,
pause,
stop
}
private int i = 0;
RunState state = RunState.stop;
public Form1()
{
InitializeComponent();
}
private void RunProc(object sender,EventArgs e)//执行的部分
{
timer1.Enabled = false;
if (state == RunState.running)
{
label1.Refresh();
label1.Text = "";
label1.Text = i.ToString();
label1.Refresh();
i++;
timer1.Enabled = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (this.button1.Text == "开始")
{
this.button1.Enabled = false;
this.button2.Enabled = true;
this.button3.Enabled = true;
timer1.Tick += new EventHandler(RunProc);
timer1.Enabled = true;
this.state = RunState.running;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (this.button2.Text == "暂停")
{
timer1.Enabled = false;
state = RunState.pause;
this.button2.Text = "继续";
}
else
{
timer1.Enabled = true;
this.state = RunState.running;
this.button2.Text = "暂停";
this.button2.Enabled = true;
//timer1.Start();
}
}
private void button3_Click(object sender, EventArgs e)
{
this.timer1.Enabled = false;
this.i = 0;
this.label1.Text = "0";
this.state = RunState.stop;
this.button1.Text = "开始";
this.button1.Enabled = true;
this.button2.Text = "暂停";
this.button2.Enabled = false;
this.button3.Enabled=false;
}
private void Form1_Load(object sender, EventArgs e)
{
this.label1.Text = "0";
this.button2.Enabled = false;
this.button3.Enabled = false;
}
}
}