Calendar Articles and Tutorials

Applying Custom Colors to Individual Cells

Author: Codejock Software
Platform: All Environments

The Calendar allows you to individually change the background color of each cell. This can be useful for tasks such as creating a custom work week mask.

To change the background color of time cells all you need to do is use the BeforeDrawDayViewCell event to change the color before the cell is drawn. The BeforeDrawDayViewCell event will be called before each time cell is drawn in Day and WorkWeek View.

The CellParams parameter of the BeforeDrawDayViewCell event contains information about the time cell that is about to be drawn such as date, day-of-week, time, selected state, and background color.

'The BeforeDrawDayViewCell event is called before each time cell is drawn
'in Day View and WorkWeek View.
Private Sub CalendarControl_BeforeDrawDayViewCell(ByVal CellParams As _
                        XtremeCalendarControl.CalendarDayViewCellParams)
    ' standard colors are
    ' non-work cell Bk = RGB(255, 244, 188)
    '     work cell Bk = RGB(255, 255, 213)
 
    'Color time cells 8am to 12pm, Monday to Friday (work hours)
    If TimeValue(CellParams.BeginTime) >= #8:00:00 AM# And _
                        TimeValue(CellParams.BeginTime) < #12:00:00 PM# _
                        And Weekday(CellParams.BeginTime) <> 1 And _
                        Weekday(CellParams.BeginTime) <> 7 Then
        CellParams.BackgroundColor = RGB(255, 255, 213)
    End If
 
    'Color time cells 2pm to 4pm, Monday to Friday (work hours)
    If TimeValue(CellParams.BeginTime) >= #2:00:00 PM# And _
                        TimeValue(CellParams.BeginTime) < #4:00:00 PM# _
                        And Weekday(CellParams.BeginTime) <> 1 And _
                        Weekday(CellParams.BeginTime) <> 7 Then
       CellParams.BackgroundColor = RGB(255, 255, 213)
    End If
 
    'Color time cells 11am to 3pm, Saturday (work hours)    
    If TimeValue(CellParams.BeginTime) >= #11:00:00 AM# And _
                        TimeValue(CellParams.BeginTime) < #3:00:00 PM# _
                        And Weekday(CellParams.BeginTime) = 7 Then
       CellParams.BackgroundColor = RGB(255, 255, 213)
    End If
End Sub

In the sample above you can see that all you need to do is look for a specific time and day-of-week, then specify the background color.

Below is the results of the code above. As you can see, a custom work week mask was created. You can use this for other useful tasks such as changing the background color of currently selected time cells.

Changed Calendar Background Colors