Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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#_C# 4.0_C# 3.0_C# 2.0 - Fatal编程技术网

如何根据C#windows应用程序中的用户输入设置计时器控制?

如何根据C#windows应用程序中的用户输入设置计时器控制?,c#,c#-4.0,c#-3.0,c#-2.0,C#,C# 4.0,C# 3.0,C# 2.0,我需要关于计时器控制的帮助,我想在用户输入后设置时间,即在表单运行的用户输入文本框后 10:30 AM然后标签上的计时器将从10:30 AM开始继续。这是一个使用基本BackGroundWorker将时钟从WinForms应用程序中输入的日期/时间提前60秒的示例。如果希望使用实际时间,请更改DateTime begin=DateTime.Parse(textbox\u 1.Text);to DateTime begin=DateTime.Now;——这不是世界上最吸引人的解决方案,但无论如何,

我需要关于计时器控制的帮助,我想在用户输入后设置时间,即在表单运行的用户输入文本框后
10:30 AM然后标签上的计时器将从10:30 AM开始继续。

这是一个使用基本BackGroundWorker将时钟从WinForms应用程序中输入的日期/时间提前60秒的示例。如果希望使用实际时间,请更改DateTime begin=DateTime.Parse(textbox\u 1.Text);to DateTime begin=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);     
      }
}
System.ComponentModel.BackGroundWorker textTime=new System.ComponentModel.BackGroundWorker();//使用System.ComponentModel使用参考;为了避免所有这些打字
private void button1\u Click(object sender,EventArgs e){//此事件可以是一个按钮单击,也可以是任何您想要启动流程的事件
textTime.DoWork+=beginTextTimer;
textTime.RunWorkerAsync();
}
私有void beginTextTimer(对象发送方,DoWorkEventArgs e){
DateTime begin=DateTime.Parse(textbox_1.Text);//假设您已经完成了验证-使用DateTime。如果不需要用户输入,现在就使用
整数秒=0;
同时(总秒数<60){
totalSeconds=DateTime.Now.Subtract(begin).totalSeconds;//由于暂停1秒,因此可以在此处增加一个整数,而不是进行日期时间计算-无论暂停时间是什么,这都有效
MethodInvoker mI=()=>{
textBox_1.Text=begin.AddSeconds(totalSeconds).ToString();
};
BeginInvoke(mI);//这两行调用回UI线程以更改textbox值
系统线程线程睡眠(1000);
}
}

使用此代码希望对您有所帮助

命名空间Windows窗体应用程序1 { 公共部分类Form1:Form { 静态Int32秒=0; 静态Int32分钟=0; 字符串m; 字符串s; 公共表格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....");
        //}
    }


}

}

请说明什么类型的windows应用程序?窗体?WPF?这像闹钟还是倒计时!它的windows窗体应用程序,是的,它像倒计时计时器:)…像用户进入上午10:35,然后应用程序继续该时间..谢谢..将尝试并让您知道:)我尝试了相同的代码它工作正常..但需要系统时间继续不连续地..我的问题是..我想根据用户输入开始时间..就像文本框10:35 AM中的用户输入一样,然后从10:35开始倒计时继续,直到用户停止或关闭应用程序:)…您想知道用户单击后的分钟和秒数吗?我发送给您的代码不会解析用户的输入(而不是系统时间)但它解析的是日期/时间。我想你只需要时间。它是在一个文本框中,晚上10:35,文本是12小时还是24小时?是:)时间将继续增加,直到用户按“结束”或关闭应用程序。。非常感谢。。我试图实现这个逻辑:)谢谢大家。。最后我得到了我的解决方案:)欢迎希望你再次加入堆栈低的
    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....");
        //}
    }


}