0

I have a textbox in my form in vb 2010 which should accept only dates and only in the format of "yyyy/mm/dd"

How to achieve this? Please help

spajce
  • 7,044
  • 5
  • 29
  • 44
user1795999
  • 301
  • 2
  • 5
  • 15

3 Answers3

1
Private Sub TextBox2_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox2.Validating
    Dim test As Date
    If Date.TryParseExact(TextBox2.Text.ToString(), "yyyy/mm/dd", _
                          System.Globalization.CultureInfo.CurrentCulture, _
                          Globalization.DateTimeStyles.None, test) Then
        MessageBox.Show("Ok")
        'TODO: ok
    Else
        e.Cancel = True
        'TODO: not ok
    End If
End Sub
spajce
  • 7,044
  • 5
  • 29
  • 44
0

If you use datepicker control then you can set to custom format as you like. In the form load event use below

DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.CustomFormat = "yyyy/mm/dd"

Let me know if it helps.

DevelopmentIsMyPassion
  • 3,541
  • 4
  • 34
  • 60
0

You can try this:

DateTime.TryParseExact(dateToTest, "yyyy/MM/dd", New CultureInfo("us-US"), DateTimeStyles.None, result) 
Zach Saucier
  • 24,871
  • 12
  • 85
  • 147