Skip to main content

How to Display the Number of Events for a Particular Day within Month View Cells

  • 2 minutes to read

This topic shows you how to calculate the total number of user events for a given date, and display the result in a month day cell.

A straightforward way to implement this task is to handle the scheduler’s OnCustomDrawContent event, allowing manual painting of day cells in the Month View.

An event’s ViewInfo parameter provides a date range for querying qualified user events that will then be counted, and in addition, it contains ViewInfo information that is necessary for rendering Month View elements.

A day’s boundaries are determined by the ViewInfo.TimeStart and ViewInfo.TimeFinish properties. To count user events for a given date, create a TcxSchedulerFilteredEventList collection and populate it with user events matching the specified period. To do this, use storage’s GetEvents method, which takes an empty collection and date bounds as parameters.

In the example, a TcxSchedulerMonthDayContentCellViewInfoAccess class is derived from the TcxSchedulerMonthDayContentCellViewInfo class. This is required to get access to the FTextRect protected property.

// ...
type
  TcxSchedulerMonthDayContentCellViewInfoAccess = class(TcxSchedulerMonthDayContentCellViewInfo);
// ...
procedure TForm1.cxScheduler1CustomDrawContent(Sender: TObject;
  ACanvas: TcxCanvas; AViewInfo: TcxSchedulerContentCellViewInfo;
  var ADone: Boolean);
var
  AList: TcxSchedulerFilteredEventList;
  ACount, I: Integer;
begin
  if not cxScheduler1.ViewWeeks.Active then
    Exit;
  AViewInfo.Draw(ACanvas);
  AList := TcxSchedulerFilteredEventList.Create;
  try
    cxSchedulerStorage1.GetEvents(AList, AViewInfo.TimeStart,
      AViewInfo.TimeFinish);
    ACount := 0;
    for I := 0 to AList.Count - 1 do
      if AList[I].IsDayEvent(AViewInfo.TimeStart) then
        Inc(ACount);
  finally
    AList.Free;
  end;
  ACanvas.Brush.Style := bsClear;
  if TcxSchedulerMonthDayContentCellViewInfo(AViewInfo).Selected then
    ACanvas.Font.Color := TcxSchedulerMonthDayContentCellViewInfo(AViewInfo).SelectionTextColor
  else
    ACanvas.Font.Color := AViewInfo.TextColor;
  with TcxSchedulerMonthDayContentCellViewInfoAccess(AViewInfo) do
    ACanvas.DrawText('(' + IntToStr(ACount) + ')', Rect(Bounds.Left + 2, FTextRect.Top, FTextRect.Right, FTextRect.Bottom), cxAlignLeft or cxAlignVCenter);
  ACanvas.Brush.Style := bsSolid;
  ADone := True;
end;

The following image shows the code execution result:

See Also