Minimax
Minimax

Reputation: 131

How to convert a MaskedTextBox value to TimeSpan?

So far I've tried this:

try
{
    TimeSpan durtime = TimeSpan.Parse(timeDur.ToString());
}
catch (FormatException)
{
    MessageBox.Show("Bad Format", timeDur.ToString());
}

timeDur is a MaskedTextBox with a mask of type __:__ (Validating type DateTime).

When I try to convert the timedur MaskedTextBox into a string format, it gives me an exception of type System.FormatException:

"The string was not recognized as a valid TimeSpan."

Upvotes: 0

Views: 2989

Answers (2)

Minimax
Minimax

Reputation: 131

I honestly have no idea why it started working now but

etimeWHP1.Value = btimeWHP1.Value.Add(TimeSpan.Parse(timeDur.Text));

This solved my problem.

Upvotes: 0

Starceaker
Starceaker

Reputation: 631

Have you considered using a DateTimePicker with Format "Time"?

This way you don't have to worry about Culture specific formatting and so on.

Upvotes: 1

Related Questions