vb.net - how to set today as a default date for time picker?
Asked Answered
A

3

5

As the properties Value of the date/time picker does not allow to enter the DateTime.Now default value, I have tried to set it in the code:

Private Sub DataFrom_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DataForm.ValueChanged

     DataFrom.Value = DateTime.Now

End Sub

It indeed shows the current date on opening the form with the date/time picker. However one cannot set any other date from the drop down calendar (one can choose a date, what means that the calendar is dropped down allowing to point a date, but after clicking the choice the date/time picker value returns to the current date).

Thank you in advance for some indications. Marek

Asclepiadaceous answered 6/12, 2011 at 22:52 Comment(0)
I
5

I believe you are setting it in the wrong place. If you are using the "Value Changed" event to set it, it will always change back because you're overriding the value that was just selected...

You should rather set it in Form Load method where it will default once.

Ivanovo answered 6/12, 2011 at 22:56 Comment(0)
F
5

You need to set the value in the Form_Load event:

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    DataFrom.Value = DateTime.Now
End Sub
Feria answered 6/12, 2011 at 22:55 Comment(0)
I
5

I believe you are setting it in the wrong place. If you are using the "Value Changed" event to set it, it will always change back because you're overriding the value that was just selected...

You should rather set it in Form Load method where it will default once.

Ivanovo answered 6/12, 2011 at 22:56 Comment(0)
K
3

You want to put that code in Form_Load:

Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
    DataFrom.Value = DateTime.Now
End Sub
Kyongkyoto answered 6/12, 2011 at 22:56 Comment(1)
Thank You for your prompt and perfect answer - that was exactly what I needed. Marek.Asclepiadaceous

© 2022 - 2024 — McMap. All rights reserved.