DateTimePicker quandry

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,886
Here is the scenario I will make it as short as I can.

I have a form with a datagrid and two datetimepickers (fromdate and todate).

When the form shows, I have set the fromdate to start -28 days back, to load data from 28 days ago up to today.

One problem. This:

Code:
 Private Sub SupportLog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dtDateTo.Format = DateTimePickerFormat.Custom
        dtDateTo.CustomFormat = "dd, MMMM, yyyy"

        dtDateFrom.Format = DateTimePickerFormat.Custom
        dtDateFrom.CustomFormat = "dd, MMMM, yyyy"

        dtDateTo.MinDate = New DateTime(1985, 6, 20)
   [B] dtDateFrom.MaxDate = DateTime.Today[/B]
        dtDateFrom.MinDate = New DateTime(1985, 6, 20)
        dtDateTo.MaxDate = DateTime.Today

        dtDateFrom.Value = DateTime.Today.AddDays(-28)
        dtDateTo.Value = DateTime.Today

    End Sub

This horrible thing is calling the
dtDateFrom.ValueChanged
event which in turn is pulling all the records on which the control has it's original default before being changed, which is today's date.

In fact one of my plagues recently has been control events on load.

Any ideas?
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,886
Seem to have solved the problem.

I just discovered that on form load, the
dtDateFrom.ValueChanged
event actually fires twice.

1. First time it does, no records are pulled because both datetime controls are set to today.
2. It then fires a second time and this time picks up -28 days. Records are loaded.
 

netcruiser

Well-Known Member
Joined
Sep 21, 2012
Messages
450
In general to avoid reacting to events when you initialise them have something like (pseudo-code):

Private Sub xxxxOnLoad
BusyInitialising = True
Try
... Intialise your components ...
Finally
BusyIntialising = False
EndSub


Private Sub xxxOnDateChange
If BusyIntiailising Then
Exit Sub
... handle ondatechange
EndSub
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,886
Thanks netcruiser This might be the solution to a hell of a lot of headaches recently.

Edit: Yup works great thanks!

Very_Nice.jpg
 
Last edited:
Top