简体   繁体   中英

C# Add Times of multiple textboxes together

I am trying to add lap times together but I first must check if there is a value in the textbox and then proceed. How do I do this? The lap time is recorded like this: 01:29:30

Here is my code:

try
{
    TimeSpan Diff1 = TimeSpan.Parse(lap1TimeTextBox.Text);
    TimeSpan Diff2 = TimeSpan.Parse(lap2TimeTextBox.Text);
    TimeSpan Diff3 = TimeSpan.Parse(lap3TimeTextBox.Text);
    TimeSpan Diff4 = TimeSpan.Parse(lap4imeTextBox.Text);
    TimeSpan Diff5 = TimeSpan.Parse(lap5TimeTextBox.Text);
    TimeSpan Diff6 = TimeSpan.Parse(lap6TimeTextBox.Text);
    TimeSpan Diff7 = TimeSpan.Parse(lap7TimeTextBox.Text);
    TimeSpan Diff8 = TimeSpan.Parse(lap8TimeTextBox.Text);
    TimeSpan Diff9 = TimeSpan.Parse(lap9TimeTextBox.Text);
    TimeSpan Diff10 = TimeSpan.Parse(lap10TimeTextBox.Text);
    TimeSpan Total = Diff1 + Diff2 + Diff3 + Diff4 + Diff5 + Diff6 + diff7 + Diff8 + Diff9 + Diff10;
    fullTimeTextBox.Text = Total.ToString();
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

I would put all listboxes in an array, and iterate through them, to minimize the repeating of code:

   TextBox[] boxes = { lap1TimeTextBox, lap2TimeTextBox, lap3TimeTextBox }; // etc
   // Get boxes with a value
   var filledBoxes = boxes.Where(b => !string.IsNullOrWhiteSpace(b.Text)); 

   // Extract timings
   var times = filledBoxes.Select(b => TimeSpan.Parse(b.Text));

   // Calculate the sum, use ticks, because Sum might not work on TimeSpan directly
   var sum = new TimeSpan(times.Sum(time => time.Ticks));

You can do this in one line, but that would make the code less readable.

I'd create method for this purpose:

TimeSpan SumAll(params TextBox[] lapTimeTextBoxes)
{
    TimeSpan result = new TimeSpan();
    TimeSpan temp = new TimeSpan();
    for(int i = 1; i < lapTimeTextBoxes.Length; i++)
    {
       if(TimeSpan.TryParse(lap6TimeTextBox.Text, Temp)) result += temp;
    }
    return result;            
}

Use it like :

fullTimeTextBox.Text = SumAll(lap1TimeTextBox, lap2TimeTextBox, lap3TimeTextBox, lap4TimeTextBox, 
lap5TimeTextBox, lap6TimeTextBox, lap7TimeTextBox, lap8TimeTextBox, lap9TimeTextBox, lap10TimeTextBox).ToString();

You could use following approach using TimeSpan.TryParse and a TimeSpan[] :

TimeSpan[] laps = new TimeSpan[10];
bool valid = TimeSpan.TryParse(lap1TimeTextBox.Text, out laps[0]);
valid = TimeSpan.TryParse(lap2TimeTextBox.Text, out laps[1]);
// ...
valid = TimeSpan.TryParse(lap10TimeTextBox.Text, out laps[9]);

TimeSpan totalLaps = TimeSpan.Zero;
foreach (TimeSpan lap in laps)
    totalLaps = totalLaps + lap;

That works since a TimeSpan is a struct and cannot be null . All in the array are initialized with TimeSpan.Zero . So all non-parsable timespans will count as zero.

I have tested it successfully with some sample values in your format.


If you have plenty of such controls it might get tedious to repeat TimeSpan.TryParse(lap1TimeTextBox.Text, out laps[0]) for every control even if is very easy, readable and testable code.

If those TextBoxes are all in the same container-control (like a Panel or the form) you could use folllowing LINQ approach:

TimeSpan sumOfAllLapTimes = LapTimePanel.Controls.OfType<TextBox>()
    .Where(txt => txt.Name.StartsWith("lap", StringComparison.InvariantCultureIgnoreCase))
    .Select(txt => txt.Text.TryGetTimeSpan())
    .Where(ts => ts.HasValue)
    .Select(ts => ts.Value)
    .Sum();

This query uses following two handy extension methods:

public static TimeSpan? TryGetTimeSpan(this string timeSpan, IFormatProvider formatProvider = null)
{
    if (timeSpan == null) throw new ArgumentNullException("timeSpan");
    TimeSpan ts;
    bool success = TimeSpan.TryParse(timeSpan, formatProvider, out ts);
    if (success) return ts;
    return null;
}

public static TimeSpan Sum(this IEnumerable<TimeSpan> timeSpans)
{
    TimeSpan sum = TimeSpan.Zero;
    foreach (TimeSpan ts in timeSpans) sum = sum + ts;
    return sum;
}

I would do it like this:

TextBox[] timeTextBoxes = this.Controls.OfType<TextBox>().Where(txt => txt.Name.StartsWith("lap")).ToArray();

TimeSpan totalTime = new TimeSpan();
foreach (TextBox txt in timeTextBoxes)
{
    TimeSpan time;
    if (TimeSpan.TryParse(txt.Text, out time))
        totalTime = totalTime.Add(time);
}

fullTimeTextBox.Text = totalTime.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM