如何根据 C# windows 应用程序中的用户条目设置定时器控制?
How do I Set Timer Control According to User entry in C# windows application?
我需要有关计时器控制的帮助,我想在用户输入后设置时间,即在表单 运行 的用户在文本框中输入后
10:30 AM 然后标签上的计时器将从 10:30 AM 开始继续..
这是使用基本 BackGroundWorker 将时钟从 WinForms 应用程序中输入的 date/time 提前 60 秒的示例。如果您更愿意使用实际时间,请更改 DateTime begin = DateTime.Parse(textbox_1.Text);到 DateTime 开始 = DateTime.Now; -- 这不是世界上最迷人的解决方案,但无论如何它都能完成工作并且值得考虑。祝你好运!
System.ComponentModel.BackGroundWorker textTime = new System.ComponentModel.BackGroundWorker();// use a reference Using System.ComponentModel; to avoid all this typing
private void button1_Click(object sender, EventArgs e){//this event could be a button click, or whatever else you want to start the process
textTime.DoWork+= beginTextTimer;
textTime.RunWorkerAsync();
}
private void beginTextTimer(object sender, DoWorkEventArgs e){
DateTime begin = DateTime.Parse(textbox_1.Text);//assumes you've already done validation - use DateTime.Now if you don't require user's input
int totalSeconds = 0;
While(totalSeconds < 60){
totalSeconds = DateTime.Now.Subtract(begin).TotalSeconds;//since you're pausing 1 second, you could just increment an integer here rather than doing a DateTime calculation - this works no matter what the pause is
MethodInvoker mI = () => {
textBox_1.Text = begin.AddSeconds(totalSeconds).ToString();
};
BeginInvoke(mI);//these two lines invoke back to the UI thread to change the textbox value
System.Threading.Thread.Sleep(1000);
}
}
使用此代码希望对您有所帮助.....
命名空间 WindowsFormsApplication1
{
public 部分 class Form1 : 表格
{
静态 Int32 秒 = 0;
静态 Int32 分钟 = 0;
串 m;
字符串 ;
public 表格 1()
{
初始化组件();
}
private void button1_Click(object sender, EventArgs e)
{
m = textBox1.Text.Substring(0, 2);
s = textBox1.Text.Substring(3, 2);
sec = Convert.ToInt32(s);
minut = Convert.ToInt32(m);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
sec++;
if(sec>59)
{
sec = 1;
minut++;
}
if(minut>59)
{
minut = 0;
}
label1.Text = minut.ToString() + ":" + sec.ToString();
//second = second + 1;
//if (second >= 10)
//{
// timer1.Stop();
// MessageBox.Show("Exiting from Timer....");
//}
}
}
}
我需要有关计时器控制的帮助,我想在用户输入后设置时间,即在表单 运行 的用户在文本框中输入后 10:30 AM 然后标签上的计时器将从 10:30 AM 开始继续..
这是使用基本 BackGroundWorker 将时钟从 WinForms 应用程序中输入的 date/time 提前 60 秒的示例。如果您更愿意使用实际时间,请更改 DateTime begin = DateTime.Parse(textbox_1.Text);到 DateTime 开始 = DateTime.Now; -- 这不是世界上最迷人的解决方案,但无论如何它都能完成工作并且值得考虑。祝你好运!
System.ComponentModel.BackGroundWorker textTime = new System.ComponentModel.BackGroundWorker();// use a reference Using System.ComponentModel; to avoid all this typing
private void button1_Click(object sender, EventArgs e){//this event could be a button click, or whatever else you want to start the process
textTime.DoWork+= beginTextTimer;
textTime.RunWorkerAsync();
}
private void beginTextTimer(object sender, DoWorkEventArgs e){
DateTime begin = DateTime.Parse(textbox_1.Text);//assumes you've already done validation - use DateTime.Now if you don't require user's input
int totalSeconds = 0;
While(totalSeconds < 60){
totalSeconds = DateTime.Now.Subtract(begin).TotalSeconds;//since you're pausing 1 second, you could just increment an integer here rather than doing a DateTime calculation - this works no matter what the pause is
MethodInvoker mI = () => {
textBox_1.Text = begin.AddSeconds(totalSeconds).ToString();
};
BeginInvoke(mI);//these two lines invoke back to the UI thread to change the textbox value
System.Threading.Thread.Sleep(1000);
}
}
使用此代码希望对您有所帮助.....
命名空间 WindowsFormsApplication1 { public 部分 class Form1 : 表格 { 静态 Int32 秒 = 0; 静态 Int32 分钟 = 0; 串 m; 字符串 ; public 表格 1() { 初始化组件(); }
private void button1_Click(object sender, EventArgs e)
{
m = textBox1.Text.Substring(0, 2);
s = textBox1.Text.Substring(3, 2);
sec = Convert.ToInt32(s);
minut = Convert.ToInt32(m);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
sec++;
if(sec>59)
{
sec = 1;
minut++;
}
if(minut>59)
{
minut = 0;
}
label1.Text = minut.ToString() + ":" + sec.ToString();
//second = second + 1;
//if (second >= 10)
//{
// timer1.Stop();
// MessageBox.Show("Exiting from Timer....");
//}
}
}
}