The VS.NET 2003 DateTimePicker Control’s Default Value

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 selectionediting. 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
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing