Calendar (C++)
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
void __fastcall TMainForm::btnNextMonthClick(TObject *Sender)
{
/* Advance a month. */
Calendar1->NextMonth();
}
void __fastcall TMainForm::btnPrevMonthClick(TObject *Sender)
{
/* Retrograde a month. */
Calendar1->PrevMonth();
}
void __fastcall TMainForm::btnNextYearClick(TObject *Sender)
{
/* Advance a year. */
Calendar1->NextYear();
}
void __fastcall TMainForm::btnPrevYearClick(TObject *Sender)
{
/* Retrograde a year. */
Calendar1->PrevYear();
}
void __fastcall TMainForm::btnNewDateClick(TObject *Sender)
{
/* Assign the Calendar a new Date. */
try
{
/* Try to set the new date. */
Calendar1->CalendarDate = StrToDateTime(edtNewDate->Text);
}
catch(Exception &E)
{
/* Show an error message. */
ShowMessage("Error setting the new date. Possible an incorrect format. The correct date format is: mm/dd/yyyy");
}
}
void __fastcall TMainForm::btnDisplayDateClick(TObject *Sender)
{
/* Show a message. */
ShowMessage(Format("Day is: %d; Month is: %d; Year is: %d",
ARRAYOFCONST((Calendar1->Day, Calendar1->Month, Calendar1->Year))));
}
void __fastcall TMainForm::Calendar1Change(TObject *Sender)
{
/* Each time the calendar is updatet, the current date is shown on the Label. */
Label1->Caption = Format("Current date is: %s", ARRAYOFCONST((DateTimeToStr(Calendar1->CalendarDate))));
/* Also update the New Date edit box. */
edtNewDate->Text = DateTimeToStr(Calendar1->CalendarDate);
}
void __fastcall TMainForm::CheckBox1Click(TObject *Sender)
{
/* Make the calendar to use the current date. */
Calendar1->UseCurrentDate = CheckBox1->Checked;
Calendar1->OnChange(MainForm);
}
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
/* Make sure we update the Calendar when creating the Form. */
Calendar1->OnChange(MainForm);
}
Uses
- Vcl.Samples.Calendar.TCalendar ( fr | de | ja )