Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB.NET
Expertise: Intermediate
Oct 18, 2005

The VS.NET 2003 DateTimePicker Control's Default Value

In some cases, the results of the use of the DateTimePicker control can be confused—implicitly initialized. This tip describes what you can do to avoid confusion.

At run time, the control displays the date and time that were:

  • Used to explicitly initialize the control
  • Current at instantiation time (default initialization)
  • Set in the code
  • Changed by the user
In the case of default initialization, the value property continues to be automatically updated even after it was displayed the first time. The control's value property is updated, but the displayed value is not updated. You can see it in the VS debugger at runtime and in the control's Properties window in the design mode. The property is updated until it is changed by the code or by the user. If the property is not changed by the time its value is retrieved, it won't match the displayed value. This can be confusing and can lead to errors.

Solutions for avoiding this situation depend on why you're using the control. The following code snippets demonstrate how these solutions can be implemented:

  • If you're using the control for selection, the control displays the initialization value, the users selection, or the result of editing by the user. Explicitly initialize the control:
    
       Me.DateTimePicker1.Value = New Date(2002, 2, 2, 22, 22, 22, 0)
    
  • If you're using the control for display, the control's value is updated and is displayed with some frequency. Add a timer to your form that will update the control with frequency of elapsed events:
      
       Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateTimePicker
          ''The following timer is a suitable one for this example.
          Friend WithEvents TimerForDateTimeControl As System.Windows.Forms.Timer
          
       Me.DateTimePicker1.Value = Now
       Me.TimerForDateTimeControl.Enabled = True
       Me.DateTimePicker1.Enabled = False
          
       Private Sub TimerForDateTimeControl_Tick(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles TimerForDateTimeControl.Tick
            ''Update the control's Value property
            DateTimePicker1.Value = Now
       End Sub
    
  • If you're using the control for display and selection, the control's value is updated and is displayed with some frequency. The user may select or edit the value at any time. The new value is displayed until the next selection\editing. Add a timer to your form that will update the control with frequency of elapsed events. Disable the timer as soon as the user edits the control's value at the first time:
    
       Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateTimePicker
          Friend WithEvents TimerForDateTimeControl As System.Windows.Forms.Timer
          Private dtLastInternallyUpdatedValue As Date
       Private valuePropertyIsBeingUpdatedByTimer As Boolean = True
          
       dtLastInternallyUpdatedValue = Now
       Me.DateTimePicker1.Value = dtLastInternallyUpdatedValue
       Me.TimerForDateTimeControl.Enabled = True
          
       Private Sub TimerForDateTimeControl_Tick(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles TimerForDateTimeControl.Tick
    
            ''Check if the user did not changed the control's Value property
            If DateTimePicker1.Value = dtLastInternallyUpdatedValue Then
                ''Set this flag to show that the property
                ''is being updated by the timer,
                valuePropertyIsBeingUpdatedByTimer = True
    
                ''The control has a value updated by the timer.
                ''Save the new (current) value for the next check.
                dtLastInternallyUpdatedValue = Now
                ''Update the control's Value property and
                ''during updating jump to the _ValueChanged event handler ...
                DateTimePicker1.Value = dtLastInternallyUpdatedValue
                ''and return here to continue.
    
                ''Reset the flag.
                valuePropertyIsBeingUpdatedByTimer = False
    
            Else
                ''The user edited the Value property.
                ''Stop the timer after the first user's input.
                TimerForDateTimeControl.Enabled = False
            End If
        End Sub
    
        Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object,_
            ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
            If valuePropertyIsBeingUpdatedByTimer Then
                ''The timer is updating the Value property.
                ''Just exit.
            Else
                ''The user edited the Value property.
                ''Stop the timer after the first user's input.
                TimerForDateTimeControl.Enabled = False
            End If
        End Sub
    
Boris Eligulashvili
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap