RAD Studio
ContentsIndex
PreviousUpNext
Customizing a grid
Name 
Description 
An encoded numeric date is fine for applications, but humans prefer to work with days, months, and years. You can provide alternate access to those elements of the stored, encoded date by creating properties.
Because each element of the date (day, month, and year) is an integer, and because setting each requires encoding the date when set, you can avoid duplicating the code each time by sharing the implementation methods for all three properties. That is, you can write two methods, one to read an element and one to write one, and use those methods to get and set all... more 
A calendar is essentially a grid with a fixed number of rows and columns, although not all the rows always contain dates. For this reason, you have not published the grid properties ColCount and RowCount, because it is highly unlikely that users of the calendar will want to display anything other than seven days per week. You still must set the initial values of those properties so that the week always has seven days, however.
To change the initial values of the component's properties, override the constructor to set the desired values. The constructor must be virtual.
Remember that... more 
You create every component the same way: create a unit, derive a component class, register it, compile it, and install it on the Tool palette. Creating a new component. 
The component library provides abstract components you can use as the basis for customized components. The most important of these are grids and list boxes. The following topics describe how to create a small one month calendar from the basic grid component, TCustomGrid: In VCL applications, the resulting component is similar to the TCalendar component on the Samples category of the Tool palette. See Specifying the palette page
As the calendar is written, the user can select a blank cell, but the date does not change. It makes sense, then, to disallow selection of the blank cells.
To control whether a given cell is selectable, override the SelectCell method of the grid.
SelectCell is a function that takes a column and row as parameters, and returns a Boolean value indicating whether the specified cell is selectable.
You can override SelectCell to return False if the cell does not contain a valid date:  
A grid control fills in its contents cell-by-cell. In the case of the calendar, that means calculating which date, if any, belongs in each cell. The default drawing for grid cells takes place in a virtual method called DrawCell.
To fill in the contents of grid cells, override the DrawCell method.
The easiest part to fill in is the heading cells in the fixed row. The runtime library contains an array with short day names, so for the calendar, use the appropriate one for each column:  
Putting numbers into the calendar involves several considerations. The number of days in the month depends on which month it is, and whether the given year is a leap year. In addition, months start on different days of the week, dependent on the month and year. Use the IsLeapYear function to determine whether the year is a leap year. Use the MonthDays array in the SysUtils unit to get the number of days in the month.
Once you have the information on leap years and days per month, you can calculate where in the grid the individual dates go. The... more 
The inherited behavior of a grid handles moving the selection in response to either arrow keys or clicks, but if you want to change the selected day, you need to modify that default behavior.
To handle movements within the calendar, override the Click method of the grid.
When you override a method such as Click that is tied in with user interactions, you will nearly always include a call to the inherited method, so as not to lose the standard behavior.
The following is an overridden Click method for the calendar grid. Be sure to add the declaration of Click... more 
Within a given month, there are two obvious ways to navigate among the days. The first is to use the arrow keys, and the other is to respond to clicks of the mouse. The standard grid component handles both as if they were clicks. That is, an arrow movement is treated like a click on an adjacent cell.
The process of navigating days consists of  
Properties are useful for manipulating components, especially at design time. But sometimes there are types of manipulations that are so common or natural, often involving more than one property, that it makes sense to provide methods to handle them. One example of such a natural manipulation is a "next month" feature for a calendar. Handling the wrapping around of months and incrementing of years is simple, but very convenient for the developer using the component.
The only drawback to encapsulating common manipulations into methods is that methods are only available at runtime. However, such manipulations are generally only cumbersome when... more 
Now that users of the calendar can change the date within the calendar, it makes sense to allow applications to respond to those changes. 
The abstract grid component, TCustomGrid, provides a large number of protected properties. You can choose which of those properties you want to make available to users of the calendar control.
To make inherited protected properties available to users of your components, redeclare the properties in the published part of your component's declaration.
For the calendar control, publish the following properties and events, as shown here:  
Note: When a user or application changes the size of a window or control, Windows sends a message called WM_SIZE to the affected window or control so it can adjust any settings needed to later paint its image in the new size. Your VCL component can respond to that message by altering the size of the cells so they all fit inside the boundaries of the control. To respond to the WM_SIZE message, you will add a message-handling method to the component.
Creating a message-handling method is described in detail in the section Creating new message handlers.
In this case,... more 
Now that you have numbers in the calendar cells, it makes sense to move the selection highlighting to the cell containing the current day. By default, the selection starts on the top left cell, so you need to set the Row and Column properties both when constructing the calendar initially and when the date changes.
To set the selection on the current day, change the UpdateCalendar method to set Row and Column before calling Refresh:  
To store the date for the calendar, you need a private field to hold the date and a runtime-only property that provides access to that date. 
For the calendar control to be useful, users and applications must have a mechanism for setting the day, month, and year. Delphi stores dates and times in variables of type TDateTime. TDateTime is an encoded numeric representation of the date and time, which is useful for programmatic manipulation, but not convenient for human use.
You can therefore store the date in encoded form, providing runtime access to that value, but also provide Day, Month, and Year properties that users of the calendar component can set at design time.
Tracking the date in the calendar consists of the... more 
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!