DateTimePicker syntax
DateTimePicker syntax
If DateTimePicker1.Value.Date = DateTimePicker1.Value.Date Then
MessageBox.Show("Please enter appropriate Starting Date.", _
"User Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
DateTimePicker1.Focus()
Return
End If
I just want to make a validation about my dateStart DTP. I want to validate if I input the date yesterday, the messagebox will pop. My syntax is wrong. Please help me. I don't know what is the code for the date today. Because my Syntax is DateTimePicker1 is equals to itself. I want DateTimePicker1 = to date today. Thank you!
dateStart
messagebox
DateTimePicker1
DateTimePicker1
=
DtPicker
In the Properties of
DateTimePicker it has a MinDate which means you can set what to be display to your DateTimePicker. If you set the MinDate today, and run your system, your system will be automatically validate the DateTimePicker, It will accept only the Present and Future date.– user7978226
Sep 4 at 2:51
DateTimePicker
MinDate
DateTimePicker
MinDate
DateTimePicker
Setting MinDate property is much cleaner / better. No code needed just properties. It should also give you validation wherein you will not need any messagebox.
– GNMercado
Sep 4 at 3:46
2 Answers
2
In the Properties of DateTimePicker it has a MinDate which means you can set what to be display to your DateTimePicker. If you set the MinDate today, and run your system, your system will be automatically validate the DateTimePicker, It will accept only the Present and Future date. But this solution will not have a Pop-up Message like your code above.
DateTimePicker
MinDate
DateTimePicker
MinDate
DateTimePicker
But if you want a pop-up message, Try this code.
If DateTimePicker1.Value < Date.Today Then
MessageBox.Show("Date cannot be before today.")
Exit Sub
Else
'Insert else here
End If
You cannot compare DateTimePicker1 to itself because it will get it's own value and compare to itself. But you can use Date.Today and compare to your DateTimePicker1.
DateTimePicker1
Date.Today
DateTimePicker1
Hope this helps. Thanks
Setting the
MinDate property is definitely the way to go. Prevention is pretty much always better than cure, i.e. prevent the user selecting an invalid value rather than telling them to fix it if they do. To be clear, you would ned to set the property in code, probably in the Load event handler, because the actual value will vary from run to run.– jmcilhinney
Sep 4 at 3:07
MinDate
Load
Yes it will, and i recommend to use the
MinDate property, But my second solution is just for suggestion only. But anyway Thanks.– user7978226
Sep 4 at 3:10
MinDate
Hello and welcome to SO
If you want to be sure that in the DateTimePicker cannot exsist a date before today, just add this little sub on your form:
DateTimePicker
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DateTimePicker1.MinDate = Date.Today
End Sub
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You want to validate the
DtPicker? For example you cannot input like date yesterday? Is that what you want?– user7978226
Sep 4 at 2:41