Timer时间控件

第一步、创建一个Windows窗体,

第二步、创建样式,在工具箱中找到TextBox和Labell、Button、timer。

第三步、改变属性的Name和 Text(就是改写名称)

第四步、排版按钮

 

Timer时间控件_第1张图片

1:使用的控制器是Label;name改为lblTime
2:使用的控制器是TextBox;Name改为txtTime
3:使用的控制器是Button;Name改为btnGet
4.  使用的控制器是Button;Name改为btnStop

属性改变为

Timer时间控件_第2张图片

第五步是获取当前时间的代码,那么我们就有写一个方法,定义一个时间的变量DateTime

private void GetTime()

        {

            string time = DateTime.Now.ToShortDateString() + "   " + DateTime.Now.ToLongTimeString();//定义一个事件

            this.txtTime.Text = time;//利用this把获取当前系统时间绑定到文本框

        }

第六步:写“获取当前系统时间”按钮的代码

private void btnGet_Click(object sender, EventArgs e)

        {

            GetTime();

            this.timer1.Start();

        }

写完这些代码我们获取到不会动时间,如果我们要获取会动的时间就用给它的样式添加上Timer控件。

private void timer1_Tick(object sender, EventArgs e)

        {

            GetTime();

        }

我们就调用GetTime代码它就动起来

第七步:如果我们要时间停下来,那么我们就要用 Button控件来写一个停下来的代码

  private void btnStop_Click(object sender, EventArgs e)

        {this.timer1.Stop();}

最后的结果是:

Timer时间控件_第3张图片

你可能感兴趣的:(Timer时间控件)