Date. IsDate instead of IsNumeric .

hi all

how can i write Date (dd/mm/yyyy)  in textbox without using Date and Time picker in vb.net in other words how can i use textbox in date format...... and ofcouse the date must be valid.

Vimal




Answer this question

Date. IsDate instead of IsNumeric .

  • Damiaan

    Thank you very much for your help.

    it shows me the date exactly in the format i wanted, but i am unable to enter/edit any date in that textbox.

    can you please give me solution for that..

    thanks

    vimal



  • KarimRadi

    Vimal_Rupera wrote:

    Thank you very much for your help.

    it shows me the date exactly in the format i wanted, but i am unable to enter/edit any date in that textbox.

    can you please give me solution for that..

    thanks

    vimal

    Hi,

    When you get the date in the textbox, click in it and then don't delete all of a single number if you can ( else you get my error message i thought you wanted date checking anyway ) , and keep the day in the range for that month. Don't try deleting the forward slash marks either as the blue highlighted bits here>>31/03/2007.

    So if the month is Jan,Mar,May,July,Aug,Oct or Dec the date is up to the 31st all other months 30 of course except for Feb 28 or 29

    It works on my computer, i can't see why it shouldn't work on yours

    Regards,

    S_DS



  • fripper

    Dave299 wrote:

    Oh come on - that is totally unusable.

    Doing the check in the textchanged event prevents you from entering a date into an empty textbox as well as making it infuriating to try and change the current date to one you want - might make an amusing game though.

    What is the point of defining an IsDate function when it already exists in VB.

    Hi,

    Fair enough, I am still learning myself.

    I didn't try looking up IsDate either.

    Regards,

    S_DS



  • Jason Wilborn

    Hi

    To parse a date to a string, pass the relevant format to the ToString method as shown already.

    To parse a string to a date, take a look at the DateTime.Parse and TryParse methods.  Please pay particular attention to the format provider and style parameters of these methods.

    Do you really need to reinvent the wheel here .... what is wrong with the DateTimePicker control

    Richard


  • Zep--

    In DateTime Picker user would have to select the date insted enter.

    and it would be much easier to enter date for user insted selecting by up and down arrows.

    Anyways Thanks for your answer.



  • RussP

     

    -----------------------------------I've updated this post. :-)

    Hi,

    Try this, it appears to work nicely. :-)

     I thought i'd emulate the IsNumeric function for a date instead.

     

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    If IsDate(TextBox1.Text) = False Then

    TextBox1.Text = System.DateTime.Now.ToShortDateString

    MsgBox("Please input a date.", , "Date only please.")

    End If

    End Sub

    Public Function IsDate(ByVal myDate As String) As Boolean

    Dim testDate As System.DateTime

    Try

    testDate = CDate(myDate)

    Return True

    Catch ex As Exception

    Return False

    End Try

    End Function

     

    Regards,

    S_DS

     

     



  • Samer Selo

    Hi, if you mean to just show the date, then try this:

    TextBox1.Text = System.DateTime.Now.ToString("dd/MM/yyyy")

  • Webbert

    Oh come on - that is totally unusable.

    Doing the check in the textchanged event prevents you from entering a date into an empty textbox as well as making it infuriating to try and change the current date to one you want - might make an amusing game though.

    What is the point of defining an IsDate function when it already exists in VB.

    Also from a usability point of view changing the incorrect date to the current date is not helpful to the user as they cannot then see what they did wrong. Much better to just highlight the entered text and let them amend it.

    Try this instead:

    Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    If IsDate(TextBox1.Text) Then
    TextBox1.Text = CDate(TextBox1.Text).ToShortDateString
    Else
    TextBox1.SelectAll()
    MessageBox.Show("Please enter a valid date")
    e.Cancel = True
    End If
    End Sub

    This will check the entry in the textbox whenever you attempt to move focus away from the textbox. It has the side effect of preventing the form from closing if you haven't entered a valid date but you can get round this (almost!) by putting e.Cancel in the FormClosing event.


  • Date. IsDate instead of IsNumeric .