从datetimepicker到文本框的总和时间

时间:2017-03-07 11:06:33

标签: c#

在Windows窗体中的c#我有五种时间戳唯一的时间格式。 这四个值写在4个文本框中。 我需要在第五个textboxtimepicker中查看pprecedenti 4的小时数。

这段代码错了:

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();
        }

    }

我必须把所有工作时间都计算在内

1 个答案:

答案 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;
   }
}