[英]How to let user enter an integer and start timer based off on that value
I am in the process of creating my first c# project, a personal time tracking application. 我正在创建我的第一个c#项目,一个个人时间跟踪应用程序。 Pretty basic so far however before I get any further I would like to have the timer working properly.
到目前为止,还算很基本,但是在我进一步了解之前,我希望计时器能够正常工作。
So far the timer will start / stop and reset. 到目前为止,计时器将开始/停止并重置。 However a curious thing that I wanted to be able to do was for the user to set a time and have the counter start from there.
但是,我想做的一件奇怪的事情是让用户设置时间并让计数器从那里开始。
So if they wanted to start at 20 minutes and have it count up, then it would 因此,如果他们想在20分钟后开始计算,那么它会
example: 00:20:00 would count from 20 and add to it. 例如:00:20:00将从20开始计数并添加到该计数中。
However so far I have not figured it out. 但是到目前为止,我还没有弄清楚。
Here is the code: 这是代码:
namespace TimeTracker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TimerBox.Text = string.Format("00:00:00");
}
int ss = 0;
public void StartButton_click(object sender, EventArgs e)
{
timer1.Start();
timer1.Enabled = true;
timer1.Interval = 1000;
}
public void StopButton_click(object sender, EventArgs e)
{
timer1.Stop();
TimerBox.Text = TimeSpan.FromSeconds(ss).ToString();
}
public void ResetButton_click(object sender, EventArgs e)
{
ss = 0;
TimerBox.Text = TimeSpan.FromSeconds(ss).ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
ss++;
TimerBox.Text = TimeSpan.FromSeconds(ss).ToString();
}
}
}
Here is the application: 这是应用程序:
http://imgur.com/VNXVrtp http://imgur.com/VNXVrtp
Any help would be appreciated, I can provide more details if you would like! 任何帮助将不胜感激,如果您愿意,我可以提供更多详细信息!
EDIT: Since it was unclear, my question is: 编辑:由于不清楚,我的问题是:
What process would be better for coding this, adding to the integer or if there is a better way of implementing this? 什么样的过程会对此编码更好,将其添加到整数中,或者是否有更好的方法实现?
You can set the initial value of that ss
variable to any predefined integer entered by user, eg 您可以将该
ss
变量的初始值设置为用户输入的任何预定义整数,例如
DateTime _dt = DateTime.Parse(TimertBox.Text);
int ss = _dt.Second + _dt.Minute * 60 + _dt.Hour * 3600;
Try something like... 尝试类似...
public partial class Form1 : Form
{
private TimeSpan Offset = new TimeSpan();
private System.Diagnostics.Stopwatch SW = new System.Diagnostics.Stopwatch();
public Form1()
{
InitializeComponent();
timer1.Interval = 1000;
UpdateTime();
}
public void StartButton_click(object sender, EventArgs e)
{
TimeSpan TS;
if (TimeSpan.TryParse(TimerBox.Text, out TS))
{
Offset = TS;
}
else
{
MessageBox.Show("Invalid Starting Time. Resetting to Zero");
Offset = new TimeSpan();
}
SW.Restart();
UpdateTime();
timer1.Start();
}
public void StopButton_click(object sender, EventArgs e)
{
SW.Stop();
timer1.Stop();
}
public void ResetButton_click(object sender, EventArgs e)
{
Offset = new TimeSpan();
if (SW.IsRunning)
{
SW.Restart();
}
else
{
SW.Reset();
}
UpdateTime();
}
private void timer1_Tick(object sender, EventArgs e)
{
UpdateTime();
}
private void UpdateTime()
{
TimerBox.Text = Offset.Add(SW.Elapsed).ToString(@"hh\:mm\:ss");
}
}
尝试正常使用计时器(从00:00:00.00开始),并且在更新输出标签/文本框等时,只需增加用户书写的时间即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.