On approximately 10/14/2004 5:13 AM, came the following characters from the keyboard of Frazier, Joe Jr:
Thanks Glenn, using the GetFocus method to verify I was in the text
field at the beginning of the text field change event was exactly what I
needed.

Yes, that should stop any sort of "endless" propagation of Change events. Glad it worked for you; I was too tired last night to attempt to show sample code, and your fragments were not a complete test bed to play with.

Also, just in case anyone else is interested, I had a stupd
logic error also.

So, having fixed the logic error, do you still need the GetFocus for correctness? The changes that you are making should only make changes that are consistent with each other. And a slight modification to only change a value if it is different than the value that you have calculated should then also limit the propagation of events, without using GetFocus.

It is always a good idea in an event model to not change something to the same value that it already has, for exactly the reason that it could spawn another cascading round of events.

I demonstrate the technique with untested code added to Start_Change below...

 Basically

my $date1 = sprintf "%04d%02d%02d", ($win->End->GetDate())[2,1,0]; my $date2 = sprintf "%04d%02d%02d", ($win->Start->GetDate())[2,1,0]; $win->tfDays->Text( $date1 - $date2); }

Is broken like crazy.  Subtracting 20040930 from 20041014 gives 84 days,
which is obviously not right. I changed to:
sub Start_Change
{
        my $date1 = Date::Simple->new(sprintf "%04d-%02d-%02d",
($win->End->GetDate())[2,1,0]);
        my $date2 = Date::Simple->new(sprintf "%04d-%02d-%02d",
($win->Start->GetDate())[2,1,0]);
        my $range = Date::Range->new($date1, $date2);
        $win->tfDays->Text( $range->length -1)

            unless $range->length -1 == $win->tfDays->Text();

}


sub End_Change
{
        my $date1 = Date::Simple->new(sprintf "%04d-%02d-%02d",
($win->End->GetDate())[2,1,0]);
        my $date2 = Date::Simple->new(sprintf "%04d-%02d-%02d",
($win->Start->GetDate())[2,1,0]);
        my $range = Date::Range->new($date1, $date2);
$win->tfDays->Text( $range->length -1); }

sub tfDays_Change
{
        return unless (Win32::GUI::GetFocus() ==
$win->tfDays->{-handle});
        my $date = sprintf "%04d-%02d-%02d",
($win->End->GetDate())[2,1,0];
        my $enddate = Date::Simple->new($date);
        my $end = $enddate - $win->tfDays->Text();
        $win->Start->SetDate(reverse(split("-", $end)));        
}

which correctly calculates 14 days when given 2004-09-30 from 2004-10-14
by using the Date::Simple and Date::Range Modules.  Doh!

Yes, using preexisting debugged modules is always a good idea for something like dates, which are well defined, but have lots of tricky logic needed to manipulate them.

--
Glenn -- http://nevcal.com/
===========================
The best part about procrastination is that you are never bored,
because you have all kinds of things that you should be doing.

Reply via email to