2016-09-17 24 views
-2

我需要更多關於定時器控制的幫助,我想設置形式運行的用戶後,即用戶輸入後時間文本框中輸入 10:30然後標籤上定時器將從10:30繼續開始..如何根據C#windows應用程序中的用戶輸入設置定時器控件?

+0

請註明什麼樣的Windows應用程序的?形式? WPF? – Alex

+0

這就像鬧鐘或倒數計時器! –

+0

它的Windows窗體應用程序和它是一樣倒計時.. :) .. **如用戶輸入10:35 AM然後應用程序繼續與時間.. ** –

回答

0

使用這個代碼希望其有幫助.....

命名空間WindowsFormsApplication1 { 公共部分Form1類:形式 { 靜態的Int32秒= 0; static Int32 minut = 0; string m; string s; public Form1() { InitializeComponent(); }

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...."); 
     //} 
    } 


} 

}

+0

非常感謝。我想實現這個邏輯:) –

+0

謝謝all..Finally我得到了我的解決辦法:) –

+0

歡迎希望你再來堆棧溢出.......... –

0

這是一個使用基本BackGroundWorker從WinForms應用程序中輸入的日期/時間提前60秒鐘的示例。如果您想使用實際時間,請更改DateTime begin = DateTime.Parse(textbox_1.Text);到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);  
     } 
} 
+0

謝謝..will嘗試,讓你知道: ) –

+0

我嘗試相同的代碼及其fine..but對其採取系統時間連續工作.. 我的問題是..我想開始時間根據用戶entry..Like用戶在文本框中輸入10:35 AM然後從倒計時開始10:35繼續,直到用戶停止或關閉應用程序:) .. –

+0

你想要多少分鐘和秒,因爲用戶點擊? –

相關問題