Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将计时器添加到Windows窗体应用程序_C#_Winforms - Fatal编程技术网

C# 将计时器添加到Windows窗体应用程序

C# 将计时器添加到Windows窗体应用程序,c#,winforms,C#,Winforms,我想添加一个计时器,而不是在 表单加载。开始时间应为45分钟,一旦结束,即达到0分钟,表单应终止并显示消息。我该怎么做 语言:最好是C#。在您的表单main中使用类似的语言。在可视化编辑器中双击表单以创建表单加载事件 Timer Clock=new Timer(); Clock.Interval=2700000; // not sure if this length of time will work Clock.Start(); Clock.Tick+=new EventHandle

我想添加一个计时器,而不是在 表单加载。开始时间应为45分钟,一旦结束,即达到0分钟,表单应终止并显示消息。我该怎么做


语言:最好是C#。

在您的表单main中使用类似的语言。在可视化编辑器中双击表单以创建表单加载事件

 Timer Clock=new Timer();
 Clock.Interval=2700000; // not sure if this length of time will work 
 Clock.Start();
 Clock.Tick+=new EventHandler(Timer_Tick);
然后添加一个事件处理程序,以便在计时器触发时执行某些操作

  public void Timer_Tick(object sender,EventArgs eArgs)
  {
    if(sender==Clock)
    {
      // do something here      
    }
  }
更详细一点:

    private void Form1_Load(object sender, EventArgs e)
    {
        Timer MyTimer = new Timer();
        MyTimer.Interval = (45 * 60 * 1000); // 45 mins
        MyTimer.Tick += new EventHandler(MyTimer_Tick);
        MyTimer.Start();
    }

    private void MyTimer_Tick(object sender, EventArgs e)
    {
        MessageBox.Show("The form will now be closed.", "Time Elapsed");
        this.Close();
    }
下载

然后在窗体上添加一个按钮或其他内容,并在其事件中打开此应用程序 即:

{

Process.Start(@“C:\ProgramFiles(x86)\Free Desktop Timer\Desktop Timer”)


}

嘿,thanx兄弟,这很有效。。。。事实上,我是prog的新手,如果你能给我解释一下它是如何工作的,我会非常感激。特别是这两行:MyTimer.Tick+=neweventhandler(MyTimer\u Tick);MyTimer.Start();为什么是1000?,也就是45*60*1000。。。。我想在表格上的标签或类似dat的东西中显示时间(倒计时)。。。ThanxTimer.Tick+=新事件处理程序(MyTimer\u Tick);当设定的时间间隔过去时,计时器有一个称为Tick的事件。在本例中,我们的意思是希望在Tick事件发生时调用MyTimer\u Tick方法。MyTimer.Interval=(45*60*1000);计时器的时间间隔是以毫秒为单位的,因此我使用了一种计算方法,而不是仅仅计算出答案并输入值。我认为这使它更容易理解。thanx…我明白了…bt wat关于在窗体上显示倒计时??如果你想显示倒计时,那么你必须使用更高粒度的计时器(例如,1秒或1分钟,取决于你的预期)并处理其中的逻辑。就我个人而言,我喜欢能够帮助完成作业。关键词是帮助。那么,为什么我们在做别人的家庭作业时却没有合理的努力呢?这是一个糟糕的解决方案。。。下载第三方计时器pffft。你为什么不使用标准的定时器控件呢?你是这个组件的作者吗?我是初学者。。但实际上,如何在运行时显示计时器控件呢?就像第三方计时器一样。。。如果您知道,请回答..私有子计时器1_勾选(发件人作为对象,例如作为事件参数)Dim diff As New TimeSpan()diff=DateTime.Now.Subtract(startTime)lblTime.Text=diff.Hours&“:”&diff.Minutes.ToString(“00”)&“&diff.Seconds.ToString(“00”)lblTime.Invalidate()End Sub in Visual studio winforms,C#?Meh,你甚至不感谢我提供了VB.Net代码。