locked
Date Format with MaskedTextbox RRS feed

  • Question

  • Ok.. I have something weird going on.. I have a datatable with column defined as

                '   Date Of Birth
                Dim DOBDataColumn As DataColumn = table.Columns.Add("DOB", GetType(Date))
                'DOBDataColumn.DefaultValue = "01/01/1939"
    
                '   Hire Date
                Dim HireDateDataColumn As DataColumn = table.Columns.Add("HireDate", GetType(Date))
                'HireDateDataColumn.DefaultValue = "01/01/1939"

    I read the data into a structure that as a DOB and HireDate defined as Date

    on my form I have to maskedtextboxed that have the mask defined as 90/90/0000 

    if I enter a date of 1/12/1993 and save the data to the table, then open from back up it comes up looking ..

    what am I doing wrong here??

    Sunday, March 27, 2016 2:12 AM

Answers

  • if I enter a date of 1/12/1993 and save the data to the table, then open from back up it comes up looking ..

    You should show the code that you are using to copy the text box text into that date field. It's likely that you are relying on a default conversion from String to Date, when you should be specifying the exact format that you want to use for the conversion.  See:

    https://msdn.microsoft.com/en-us/library/2h3syy57(v=vs.110).aspx


    Sunday, March 27, 2016 3:06 AM
  • thanks, I was thinking something along those lines.. I was doing a txtDOB.text = cdate(.DOB)

    dugh.. .DOB is already a date.'

    did this

    .DOB.ToString("MM/dd/yyyy")

    thanks

    Sunday, March 27, 2016 3:43 AM

All replies

  • if I enter a date of 1/12/1993 and save the data to the table, then open from back up it comes up looking ..

    You should show the code that you are using to copy the text box text into that date field. It's likely that you are relying on a default conversion from String to Date, when you should be specifying the exact format that you want to use for the conversion.  See:

    https://msdn.microsoft.com/en-us/library/2h3syy57(v=vs.110).aspx


    Sunday, March 27, 2016 3:06 AM
  • thanks, I was thinking something along those lines.. I was doing a txtDOB.text = cdate(.DOB)

    dugh.. .DOB is already a date.'

    did this

    .DOB.ToString("MM/dd/yyyy")

    thanks

    Sunday, March 27, 2016 3:43 AM
  • Hi LandLord324,

    Use .DOB.ToString("MM/dd/yyyy") would be fine. In addition, I suggest you add TypeValidationCompleted event handle to validate user input. Code below is for your reference.

    MaskedTextBox1.ValidatingType = GetType(System.DateTime)
    
    Private Sub MaskedTextBox1_TypeValidationCompleted(sender As Object, e As TypeValidationEventArgs) Handles MaskedTextBox1.TypeValidationCompleted
        If Not e.IsValidInput Then
            MessageBox.Show("The value you entered is not a valid date. Please change the value.")
            e.Cancel = True
        End If
    End Sub
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 28, 2016 5:26 AM