0

in my application i have a song class which has a property length and this looks like:

[DisplayFormat(DataFormatString = "{0:mm\\:ss}", ApplyFormatInEditMode = true)]
[RegularExpression(@"[0-6][0-9]\:[0-5][0-9]", ErrorMessage = "Invalid Time.")]
public TimeSpan? Length { get; set; }

so i want it to display mm:ss and it works like a charm but as i want to validate my input that doesn't work as good. i also want the user to input the length as mm:ss, so that is forced by the regex. but my application interprets the minutes as hours and the seconds as minutes.

so when i enter 05:25, my length will be set to 05:25:00. how can i change this? i'm just a beginner so i appologize if this is a stupid question. thanks in advance

June7
  • 19,874
  • 8
  • 24
  • 34
freshrebel
  • 99
  • 1
  • 12
  • `DisplayFormat` only affects the way the stored data is displayed. In particular, it **does not affect the way the entered data is parsed**. I'm afraid what you want to do cannot be done easily. I have closed this question as a duplicate of another one, whose answer explains exactly why this is the case. – Heinzi Nov 02 '17 at 16:41
  • When you enter `05:25` *where*? TimeSpan has no format, it's just a binary value. The problem is caused in whatever code you have that parses the input into a TimeSpan. Are you using data binding? You should set the format string on the data binding statement then – Panagiotis Kanavos Nov 02 '17 at 16:45

0 Answers0