locked
MaskedTextBox function and KeyPress restricted user entry RRS feed

  • Question

  • Hi

    I have the following code. I need to test an entry in the textbox to check if entry is a valid date.

    - I would like to use the MaskedTextBox function in this click event and then limit the entry to date only in the KeyPress event.

    Private Sub CheckDate_Validating(sender As Object, e As EventArgs) Handles CheckDate.MouseClick
    Dim dt As String = DateTime.Now

    Format(dt, "MM/dd/yy")


    ' Configure the message box to be displayed
    Dim messageBox1Text As String = "is Not a valid date"
    Dim messageBox2Text As String = "IS a valid date"
    If Date.TryParseExact(txtDoj.Text.ToString(), "MM/dd/yyyy", System.Globalization.CultureInfo.CurrentCulture, Globalization.DateTimeStyles.None, dt) Then
    ' Display message box
    MessageBox.Show(messageBox2Text)


    Else
    ' Display message box
    MessageBox.Show(messageBox1Text)
    End

    End If


    End Sub


    Private Sub txtDoj_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDoj.KeyPress


    End Sub


    Please help. Thanks
    Wednesday, March 19, 2014 7:49 AM

Answers

  • Hi, I think it is better to use DateTimePicker, you can input date in DateTimePicker and you don't need to write any code to check the date.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker(v=vs.110).aspx

    • Proposed as answer by IronRazerz Wednesday, March 19, 2014 4:46 PM
    • Marked as answer by Carl Cai Tuesday, March 25, 2014 6:54 AM
    Wednesday, March 19, 2014 2:36 PM
  • Hi,

     I agree with lapheal that a DateTimePicker would be a much better choice for letting the user select a date. The user would not be able to enter an invalid date. Also if you need to limit the date that they pick such as not being able to select a date before the current date or after a certain date then you can set the MinDate and/or the MaxDate properties of the DateTimePicker to easily do that. Something like this.

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            'Don`t allow a date before the current date to be selected if you want
            DateTimePicker1.MinDate = Now
    
            'Sets the maximum date that can be selected to 1 month from current date if a maximum date is needed
            'There is also (.AddYears) and (.AddDays) or you can set a specific date using a Date structure
            DateTimePicker1.MaxDate = Now.AddMonths(1)
    
        End Sub
    

    • Proposed as answer by Carl Cai Monday, March 24, 2014 4:42 AM
    • Marked as answer by Carl Cai Tuesday, March 25, 2014 6:54 AM
    Wednesday, March 19, 2014 5:02 PM

All replies

  • Hi, I think it is better to use DateTimePicker, you can input date in DateTimePicker and you don't need to write any code to check the date.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker(v=vs.110).aspx

    • Proposed as answer by IronRazerz Wednesday, March 19, 2014 4:46 PM
    • Marked as answer by Carl Cai Tuesday, March 25, 2014 6:54 AM
    Wednesday, March 19, 2014 2:36 PM
  • Hi,

     I agree with lapheal that a DateTimePicker would be a much better choice for letting the user select a date. The user would not be able to enter an invalid date. Also if you need to limit the date that they pick such as not being able to select a date before the current date or after a certain date then you can set the MinDate and/or the MaxDate properties of the DateTimePicker to easily do that. Something like this.

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            'Don`t allow a date before the current date to be selected if you want
            DateTimePicker1.MinDate = Now
    
            'Sets the maximum date that can be selected to 1 month from current date if a maximum date is needed
            'There is also (.AddYears) and (.AddDays) or you can set a specific date using a Date structure
            DateTimePicker1.MaxDate = Now.AddMonths(1)
    
        End Sub
    

    • Proposed as answer by Carl Cai Monday, March 24, 2014 4:42 AM
    • Marked as answer by Carl Cai Tuesday, March 25, 2014 6:54 AM
    Wednesday, March 19, 2014 5:02 PM