我需要有关计时器控制的帮助,我想在用户输入后设置时间,即在表单运行后用户输入文本框 上午10:30,标签上的计时器将从上午10:30开始继续..
答案 0 :(得分:0)
这是一个使用基本BackGroundWorker从WinForms应用程序中输入的日期/时间提前60秒的示例。如果您宁愿使用实际时间,请更改DateTime begin = DateTime.Parse(textbox_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);
}
}
答案 1 :(得分:0)
使用此代码希望它有用.....
命名空间WindowsFormsApplication1 { 公共部分类Form1:表格 { static Int32 sec = 0; static Int32 minut = 0; 串m; 字符串s; 公共Form1() { 的InitializeComponent(); }
<ul class="service">
<li>
<ul class="service-sub">
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Acura Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Chrysler Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Honda Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Jeep Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Mercedes-Benz Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Porche Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Subaru Repair</li>
</ul>
</li>
<li>
<ul class="service-sub">
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Acura Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Chrysler Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Honda Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Jeep Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Mercedes-Benz Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Porche Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Subaru Repair</li>
</ul>
</li>
<li>
<ul class="service-sub">
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Acura Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Chrysler Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Honda Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Jeep Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Mercedes-Benz Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Porche Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Subaru Repair</li>
</ul>
</li>
<li>
<ul class="service-sub">
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Acura Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Chrysler Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Honda Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Jeep Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Mercedes-Benz Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Porche Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Subaru Repair</li>
</ul>
</li>
<li>
<ul class="service-sub">
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Acura Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Chrysler Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Honda Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Jeep Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Mercedes-Benz Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Porche Repair</li>
<li><span><img src="http://localhost/demo/wp-content/uploads/2016/09/serimg-1.png" alt="serimg" width="24" height="18" class="alignnone size-full wp-image-1139"></span>Subaru Repair</li>
</ul>
</li>
</ul>
}