How to ensure dragged/resized allocations always start on the first day of a week and finish on the last day of a week.


It's no surprise to discover that this is example is very similar to the previous one. The IsoDayOfWeek function can be found in JCLDateTime from the JEDI code library.

procedure TForm1.RACAllocationDrag(Sender: TObject;
                             Resource: TssResource;
                             Allocation: TssResourceAllocation;
                             var StartDateTime,
                                 EndDateTime: TDateTime,
                             var AllowDrag: Boolean);
var
  Duration, NextWeek, ThisWeek: TDateTime;
  dow: integer;
begin
  Duration := EndDateTime - StartDateTime;
  // allocations must always start on first day of week
  dow := IsoDayOfWeek(StartDateTime); // from JCL
  if dow <> 1 then begin
    // find the neareast week - ahead or not?
    ThisWeek := StartDateTime - dow + 1;
    NextWeek := ThisWeek + 7;
    if NextWeek - StartDateTime < StartDateTime - ThisWeek then begin
      StartDateTime := NextWeek;
      EndDateTime := NextWeek + round(Duration);
    end else begin
      StartDateTime := ThisWeek;
      EndDateTime := ThisWeek + round(Duration);
    end;
  end;
end;

procedure TForm1.RACAllocationStretch(Sender: TObject;
                             Resource: TssResource;
                             Allocation: TssResourceAllocation;
                             var StartDateTime,
                                 EndDateTime: TDateTime,
                             var AllowDrag: Boolean);
var
  dow: integer;
begin
  dow := IsoDayOfWeek(StartDateTime); // from JCL
  if dow <> 1 then begin
    StartDateTime := StartDateTime - dow + 1;
    if dow > 3 then // nearly next week
      StartDateTime := StartDateTime + 7;
  end;
  dow := IsoDayOfWeek(EndDateTime); // from JCL
  if dow <> 1 then begin
    EndDateTime := EndDateTime - dow + 1;
    if dow > 3 then // nearly next week
      EndDateTime := EndDateTime + 7;
  end;
end;

Note: These event handlers can be invoked many times a second while the user is dragging or re-sizing an allocation. It is important that they complete quickly in order to maintain a responsive user experience.

What People Say

Thanks for the component. It's quite amazing, powerful and unique.
Raymond Kennington, Programming Solutions (see the full quote)
More …

No malware No spam
McAfee Site Report
Web of Trust