VCL Controls: The Month Calendar |
|
Introduction |
The Win32 API provides a control used to select dates on a colorful calendar. The dates used and the way they display are based on the Regional Settings of the Control Panel. It may also depend on the operating system: This convenient control is called Month Calendar. The title bar of the control displays two buttons and two labels. The left button allows the user to select the previous month by clicking the button. The left label displays the currently selected month. The right label displays the year of the displayed date. The right button is used to get to the next month. |
The calendar can be configured to display more than one month. Here is an example that displays two months: If the control is displaying more than one month, the buttons would increment or decrement by the previous or next month in the list. For example, if the control is displaying April and May, if the user clicks the left button, the control would display March and April. If the control is displaying April and May and the user clicks the right button, the control would display May and June. Also, to select any month of the current year, the user can click the name of the month, which displays the list of months and this allows the user to click the desired month: To select a year, the user clicks the year number. This changes the year label into a spin button: To change the year, the user can click the up or down
arrows of the spin button. As the spin button is displaying, the user can
also use the arrow keys of the keyboard to increase or decrease the value.
|
Practical Learning: Starting the Exercise |
//--------------------------------------------------------------------------- void __fastcall TfrmMain::FormCreate(TObject *Sender) { edtStartPeriod->Text = Date(); edtEndPeriod->Text = Date() + 13; } //--------------------------------------------------------------------------- |
Month Calendar Properties |
To create a calendar, add the MonthCalendar button to a form or container. The MonthCalendar object is created from the TMonthCalendar class which is indirectly derived from TWinControl. The MonthCalendar control is a rectangular object without a border. After placing it on the form, it displays the current month and only one month. This is because, by default, its width and height are set enough to accommodate a month. To display more than one month, change the width of the control to provide enough space: In the same way, you can increase the height to display many months. Of course, you can programmatically change these colors. Although any color is allowed in any category, you should make sure that the calendar is still reasonably appealing and usable. |
/--------------------------------------------------------------------------- void __fastcall TForm1::MonthCalendar1Click(TObject *Sender) { Label1->Caption = MonthCalendar1->Date; } //---------------------------------------------------------------------------
When the user clicks the MonthCalendar control, one date is selected. To control whether the user can select one or more dates, set the value of the
MultiSelect property accordingly. For example, if you want the user to select a range of dates on the control, set the
MultiSelect property to true. |
Practical Learning: Adding a Calendar Control |
|
Calendar Methods and Events |
The TMonthCalendar class provides a constructor that can be used to programmatically create a calendar and place it on a form or container. To do this, declare a pointer to TMonthCalendar and, using the new operator, specify a the container and the parent of the control. Here is an example: |
//--------------------------------------------------------------------------- #include <vcl.h> #include <ComCtrls.hpp> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { TMonthCalendar *MCal = new TMonthCalendar(this); MCal->Parent = this; } //---------------------------------------------------------------------------
After creating the control, all of the properties we have seen above can be accessed. void __fastcall BoldDays(const unsigned * Days, const int Days_Size, unsigned &MonthBoldInfo); This method can be used to format some days in bold and it is mostly used in conjunction with the OnGetMonthInfo() event. The only event the MonthCalendar control handles on its own is the OnGetMonthInfo() event. Its syntax is: void __fastcall OnGetMonthInfo(TObject *Sender, DWORD Month, DWORD &MonthBoldInfo) This event fires as soon as the month of the calendar has been changed. As a descendant of TWinControl, the MonthCalendar control fires the same regular events of a Windows control. |
Practical Learning: Using a Month Calendar Control |
|
|
||
Home | Copyright © 2004-2014 FunctionX, Inc. | |
|