Re: TextBox Validation for time only, not date

Tech-Archive recommends: Fix windows errors by optimizing your registry



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


.



Relevant Pages

  • Re: TextBox Validation for time only, not date
    ... Private Sub TxtJanlight1on1_Validating(ByVal sender As Object, ... The default conversion from String to DateTime assumes the 1/1/0001 if no date part is entered. ... the TimeSpan type should be used. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Can you have one that is just mm/dd or mm/yyyy etc?
    ... need to store it as a DateTime of December 2001. ... Why is storing the date as a String not an option? ... Another way to go would be store an arbitrary day with your month and year ...
    (comp.lang.java.programmer)
  • Re: Are DateTime datatypes worth the aggravation?
    ... Although I am probably one of the most datetime puritans in these ... the range of datetime you can't even store Columbus's voyage to America ... Yes/No or what have you and as such are cast to strings. ... from string to datetime seem trivial. ...
    (microsoft.public.dotnet.general)
  • Re: DateTime short formatted range lookup
    ... Why are you storing the date as a string in the database? ... It is stored as datetime. ... That is of course if you change the database so that you store the date ...
    (microsoft.public.dotnet.languages.vb)
  • Date: Can you have one that is just mm/dd or mm/yyyy etc?
    ... need to store it as a DateTime of December 2001. ... Storing it as a String ...
    (comp.lang.java.programmer)