Calendar (Delphi)
Contents
Description
This code shows the use of the TCalendar component. It uses a Calendar, some buttons, a checkbox, an edit box and some labels. Basic calendar cycling functionality is covered by the btnNextMonth, btnPrevMonth, btnNextYear, and btnPrevYear buttons.
The edtNewDate checkbox allows you to enter a new date and force the Calendar to display it, on the fly. The correct date format is mm/dd/yyyy
.
The btnDisplayDate button displays a message on the screen with the date picked up from the Calendar by its components (day, month, and year).
Code
procedure TMainForm.btnNextMonthClick(Sender: TObject);
begin
{ Advance a month. }
Calendar1.NextMonth;
end;
procedure TMainForm.btnPrevMonthClick(Sender: TObject);
begin
{ Retrograde a month. }
Calendar1.PrevMonth;
end;
procedure TMainForm.btnNextYearClick(Sender: TObject);
begin
{ Advance a year. }
Calendar1.NextYear;
end;
procedure TMainForm.btnPrevYearClick(Sender: TObject);
begin
{ Retrograde a year. }
Calendar1.PrevYear;
end;
procedure TMainForm.btnNewDateClick(Sender: TObject);
begin
{ Assign the Calendar a new Date. }
try
{ Try to set the new date. }
Calendar1.CalendarDate := StrToDateTime(edtNewDate.Text);
except
{ Show an error message. }
Application.MessageBox('Error setting the new date. Possible an incorrect format.' +
#13#10 + 'The correct date format is: mm/dd/yyyy', 'Error', mb_Ok);
end;
end;
procedure TMainForm.btnDisplayDateClick(Sender: TObject);
begin
{ Show a message. }
Application.MessageBox(PChar(Format('Day is: %d' + #13#10 + 'Month is: %d' + #13#10 +
'Year is: %d', [Calendar1.Day, Calendar1.Month, Calendar1.Year])),
'Error', mb_Ok);
end;
procedure TMainForm.Calendar1Change(Sender: TObject);
begin
{ Each time the calendar is updatet, the current date is shown on the Label. }
Label1.Caption := Format('Current date is: %s', [DateTimeToStr(Calendar1.CalendarDate)]);
{ Also update the New Date edit box. }
edtNewDate.Text := DateTimeToStr(Calendar1.CalendarDate);
end;
procedure TMainForm.CheckBox1Click(Sender: TObject);
begin
{ Make the calendar to use the current date. }
Calendar1.UseCurrentDate := CheckBox1.Checked;
Calendar1.OnChange(Self);
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
{ Make sure we update the Calendar when creating the Form. }
Calendar1.OnChange(Self);
end;
Uses
- Vcl.Samples.Calendar.TCalendar ( fr | de | ja )