且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows窗体不显示C#计时器

更新时间:2023-02-08 19:06:48

进入Windows窗体工具箱.在组件"下,找到计时器".将其拖放到您的表单上.它不会显示在您放置它的位置(它是不可见的),但是会显示在下面的窗格中.

Go into the Windows Forms toolbox. Under "Component", find "Timer". Drag/drop one onto your form. It won't show up where you dropped it (it's non-visible), but it will show up in a pane below.

转到新计时器的属性(默认情况下命名为"timer1")并更改:

Go to the properties of your new timer (named "timer1" by default) and change:

  • 启用 true
  • 间隔 1000 毫秒,即一秒
  • Enable to true
  • Interval to 1000 milliseconds, i.e., one second

双击表单设计器上的timer1 Timer组件(在底部).这将为默认事件(Tick)创建一个处理程序.

Double-click on the timer1 Timer component on your form designer (at the bottom). That will create a handler for the default event (Tick).

使代码看起来像这样:

 private int _count = 0;
 private void timer1_Tick(object sender, EventArgs e)
 {
     ++_count;
     Tlbl_Timer.Text = _count.ToString();
 }

您应该看到标签从1开始计数,一直到溢出为止(大约20亿).

You should see your label start counting at 1 and going up until it overflows (somewhat north of two billion).