locked
Date Time Picker Issue RRS feed

  • Question

  •  

    How to refresh a date time picker control continuously at runtime ?

     

    Normally i place a date time picker control, the time is not getting updated. I want to set the time to tick continuosly? Any body can help?

    Friday, February 15, 2008 9:18 AM

Answers

  • Instead of setting it to now (which will take the current time) you can use it like this:

     

    Code Snippet

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

    DateTimePicker1.MaxDate = Today.Add(New TimeSpan(23, 59, 59))

    End Sub

     

     

    Friday, February 15, 2008 12:41 PM

All replies

  • You can add a timer to your form and then add the following code for the timer tick event:

     

    Code Snippet

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    DateTimePicker1.Value = Now

    End Sub

     

     

     

    Make sure to set the Enabled property of the timer to TRUE and don't make the interval too short. 1000 ms should be enough in that case (the default is 100).

     

    Cheers, Daniel

     

    Friday, February 15, 2008 9:42 AM
  •  

    It works fine, but does not meet my requirement. My requirement infact is i have datetimepicker control and upon this control i have a text box which holds the selected date and time. Now ;1) I should be able to select previous days with this 2) if i select the previous day, the text box should get the previous day and time would be current time as we cannot determine previous day at what time a particular transaction was done.
    Friday, February 15, 2008 10:13 AM
  • I don't get you 100%, but by doing it like this you can select a previous date and save it to a textbox and still have the datetimepicker showing the current time.

    Note that if you select a date in the datetimepicker (eg yesterday) it will take the previous date with the current time, because you cannot actually select a time in the calendar.

    If that does not meet your goal please try to explain your problem a bit more in detail:

     

    Code Snippet

    Dim manualSelection As Boolean = True

     

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    manualSelection = False

    DateTimePicker1.Value = Now

    manualSelection = True

    End Sub

     

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

    If manualSelection Then txtPicker.Text = DateTimePicker1.Value

    End Sub

     

     

     

    Friday, February 15, 2008 11:17 AM
  •  

    This is exactly what i was looking for. it works fine but i faced with one more issue. I will not allow the users to select date more than current date. So I had to set the max date property to now on form load. So with this i am facing an issue like '2/14/2008 5:53:1 PM' is not a valid value for 'Value'. 'Value' should be between 'MinDate' and 'MaxDate'. Any solution?
    Friday, February 15, 2008 12:22 PM
  • Instead of setting it to now (which will take the current time) you can use it like this:

     

    Code Snippet

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

    DateTimePicker1.MaxDate = Today.Add(New TimeSpan(23, 59, 59))

    End Sub

     

     

    Friday, February 15, 2008 12:41 PM
  •  

    I dont know why.. .because of the timer i think all my events are firing without waiting ?
    Friday, February 15, 2008 1:15 PM
  • Sorry mate, but I don't get your question right.

     

    If you have code based on the ValueChanged event of the datetimepicker you have to put it inside the If manualSelection block in order to be skipped when the timer is fired like this:

     

    Code Snippet

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

    If manualSelection Then

    txtPicker.Text = DateTimePicker1.Value

    ' do some more stuff

    End If

    End Sub

     

     

    If this is not the case then please reformulate the question...

    Friday, February 15, 2008 2:15 PM