Re: TextBox Validation for time only, not date
- From: Ian Andrews <IanAndrews@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 4 Jan 2009 07:09:00 -0800
Thank you for your help here - what your saying does make a great deal of
sense. My only problem is what would this look like in place of my code.
Being new to VB2008, have only been able to work with code samples so far -
struggle to write my own with understanding
Many Thanks
"Armin Zingler" wrote:
Ian Andrews wrote:.
Have used to following to validate a textbox, but this accepts dates
as being ok. I only want Time values
Private Sub TxtJanlight1on1_Validating(ByVal sender As Object, ByVal
e As System.ComponentModel.CancelEventArgs) Handles
TxtJanlight1on1.Validating
If TxtJanlight1on1.Text <> "" AndAlso Not
IsDate(TxtJanlight1on1.Text) Then
MsgBox("Please enter a Valid Time Format, MUST BE AS
07:30:00, Value has been defaulted to 00:00:00")
e.Cancel = True
TxtJanlight1on1.Text = "00:00:00"
End If
End Sub
What can I do to reject dates and only accept valid time values ?
A DateTime value is any point in time. None of them has no date. The default
conversion from String to DateTime assumes the 1/1/0001 if no date part is
entered. The String "15:47" is converted to the DateTime value 1/1/0001
15:47. You can compare the date part of the DateTime value to 1/1/0001 to
find out if a date has been entered. If yes, the input was invalid, too.
To store a time only, the TimeSpan type should (can) be used. It is equal to
the type of a DateTime's TimeOfDay property which only returns the time
part. You can use the TimeSpan.Parse or .TryParse methods to convert from a
String, but note that it expects a fixed format (see [F1]) and disregards
the current culture settings.
Another way is using DateTime.ParseExact where you can pass the accepted
format(s). However, the date part will still be 1/1/0001. Therefore I'd
still store the time in a TimeSpan object. See also:
http://msdn.microsoft.com/en-us/library/2h3syy57.aspx
Armin
- Follow-Ups:
- Re: TextBox Validation for time only, not date
- From: Armin Zingler
- Re: TextBox Validation for time only, not date
- References:
- TextBox Validation for time only, not date
- From: Ian Andrews
- Re: TextBox Validation for time only, not date
- From: Armin Zingler
- TextBox Validation for time only, not date
- Prev by Date: Application_Start not firing
- Next by Date: Re: newbie: ArrayList memory leak?
- Previous by thread: Re: TextBox Validation for time only, not date
- Next by thread: Re: TextBox Validation for time only, not date
- Index(es):
Relevant Pages
|