2017-03-07 127 views
1

c#在Windows窗體中我有五個timepicker唯一的時間格式。 四個值寫在4個文本框中。 我需要在第五textboxtimepicker,以查看的pprecedenti小時總和4.總計時間從datetimepicker到文本框

這個代碼是錯誤的:

TimeSpan result = this.dateTimePicker22.Value + this.dateTimePicker23.Value + this.dateTimePicker24.Value + this.dateTimePicker25.Value + this.dateTimePicker26.Value); 
this.textBox21.Text = result.ToString(); 

餘計算的工作時間,從而:

private void Calcolaweek1() 
    { 
     textBox23.MaxLength = 5; 
     DeleteChars(); 
     if (textBox23.Text.Length == 2) 
      textBox23.Text += ":"; 

     textBox23.SelectionStart = textBox23.Text.Length; 

     DateTime time = new DateTime(); 
     this.textBox23.Text = time.ToString("HH:mm"); 


     if ((textBox1.Text.Length > 0) && (textBox2.Text.Length > 0) && (textBox3.Text.Length > 0) && (textBox4.Text.Length > 0)) 
     { 
      textBox23.MaxLength = 5; 
      TimeSpan result = this.dateTimePicker4.Value - this.dateTimePicker1.Value - (this.dateTimePicker3.Value - this.dateTimePicker2.Value); 
      this.textBox23.Text = result.ToString(); 

     } 
     else if ((string.IsNullOrEmpty(textBox2.Text)) || (string.IsNullOrEmpty(textBox3.Text))) 
     { 

      TimeSpan result1 = this.dateTimePicker4.Value - this.dateTimePicker1.Value; 
      this.textBox23.Text = result1.ToString(); 
     } 

    } 

我必須總計所有工作時間天

+0

爲什麼你使用++ –

+0

對不起。我錯了。我只用「+」 – aricbetta

+0

你有什麼例外? –

回答

0

enter image description here

private void btnCalculate_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     int hours = picker1.Value.Hour % 12 + 
        picker2.Value.Hour % 12 + 
        picker3.Value.Hour % 12 + 
        picker4.Value.Hour % 12 + 
        picker5.Value.Hour % 12; 

     int minutes = picker1.Value.Minute % 60 + 
        picker2.Value.Minute % 60 + 
        picker3.Value.Minute % 60 + 
        picker4.Value.Minute % 60 + 
        picker5.Value.Minute % 60; 

     TimeSpan fromMinutes = TimeSpan.FromMinutes(minutes); 
     var ts = hours + fromMinutes.Hours + ":" + fromMinutes.Minutes; 
    } 
    catch (Exception) 
    { 
     throw; 
    } 
} 
+0

我可以有幾分鐘嗎? HH:mm – aricbetta

+0

你是什麼意思** HH:mm ** @aricbetta?在你的問題你說,你想添加小時和分鐘和** DateTimePicker **控制有這些屬性在我的答案.Hour是你的HH和.Minute是你的MM。還有什麼你需要知道的? –

+0

我有五個datetimepicker「07:38:00」,我想獲得「38:10:00」。 – aricbetta