C#VISUAL STUDIO 10s增量计时器 [英] C# VISUAL STUDIO 10s increment timer

查看:124
本文介绍了C#VISUAL STUDIO 10s增量计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编码新手,并在各处寻找一个例子。我需要创建一个程序,以10秒的间隔递增标签。一旦标签中的数字达到60秒,我需要将其传递到分钟标签(00:00:60到00:01:00),依此类推。



一旦确定时间,点击一个开始按钮,计时器倒计时到00:00:00。



该程序是微波炉,如果它在视觉上有所帮助。

任何帮助都会很棒,谢谢!



我尝试过:



// button1是10s增量。 label4是秒。 label5将是分钟。

private void button1_Click_1(object sender,EventArgs e)

{

int counter = int.Parse(label4.Text );

counter = counter + 10;

label4.Text = counter.ToString();

New to coding and have looked everywhere for an example of this. I need to create a program that increments a label in 10s intervals. Once the number in the label has reached 60s I need it to pass over into the minutes label ("00:00:60" to "00:01:00") and so on.

Once a time has been decided, a start button is clicked and a timer counts down to 00:00:00.

The program is a microwave if that helps visually.
Any help would be amazing, thanks!

What I have tried:

//button1 is the 10s increment. label4 is seconds. label5 would be minutes.
private void button1_Click_1(object sender, EventArgs e)
{
int counter = int.Parse(label4.Text);
counter=counter+10;
label4.Text = counter.ToString();

推荐答案

从计时器开始,不用担心计数。

相反,试试这个:

Start with a timer, and don't worry about counting.
Instead, try this:
private DateTime startedAt;
private void StartTimer()
    {
    Timer ShowElapsedTime = new Timer();
    ShowElapsedTime.Interval = 10000;     // 10 seconds
    ShowElapsedTime.Tick += ShowElapsedTime_Tick;
    ShowElapsedTime.Start();
    startedAt = DateTime.Now;
    }

void ShowElapsedTime_Tick(object sender, EventArgs e)
    {
    TimeSpan diff = DateTime.Now - startedAt;
    txtHours.Text = diff.Hours.ToString();
    txtMinutes.Text = diff.Minutes.ToString();
    txtSeconds.Text = diff.Seconds.ToString();
    }


部分问题在于您使用控件(特别是标签)来保存数据。不要那样做。控件永远不应该是数据模型的一部分。它们用于编辑和显示模型中的数据,而不是您的数据模型。
Part of your problem is that you're using the controls (specifically a label) to hold your data. Don't do that. The controls should never be a part of your data model. They are there to edit and display the data in your model, not be your data model.


这篇关于C#VISUAL STUDIO 10s增量计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆