Back to Devexpress

How to: Draw Day Numbers in Different Colors

windowsforms-113746-controls-and-libraries-scheduler-examples-datenavigator-how-to-draw-day-numbers-in-different-colors.md

latest13.8 KB
Original Source

How to: Draw Day Numbers in Different Colors

  • Nov 13, 2018
  • 7 minutes to read

This example demonstrates how you can specify working days in a week, add national holidays to the calendar and display them in the DateNavigator control in a different color.

In Kuwait, weekly holidays are Friday and Saturday. Sunday to Thursday are working days. Create an array of national holidays and use the SchedulerControl.WorkDays property to add the required weekdays and holidays to the scheduler.

To change the color of a particular date in the DateNavigator control, handle the CustomDrawDayNumberCell event and draw the cell background and text using GDI drawing tools.

To change the header captions for holidays, handle the SchedulerControl.CustomDrawDayHeader event.

Run the project to see the holidays highlighted as illustrated in the image below.

View Example

csharp
using DevExpress.Schedule;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraScheduler;
using DevExpress.XtraScheduler.Drawing;
using System;
using System.Drawing;

namespace DisplayCustomHolidays {
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            // Specify a custom cell style provider to highlight cells that meet certain criteria.
            this.dateNavigator1.CellStyleProvider = new CustomCellStyleProvider(this.dateNavigator1);
            schedulerControl1.BeginUpdate();
            schedulerControl1.OptionsView.FirstDayOfWeek = FirstDayOfWeek.Sunday;
            schedulerControl1.WorkDays.Clear();
            // Specify working days of a week. Friday and Saturday are weekend holidays.
            schedulerControl1.WorkDays.Add(DevExpress.XtraScheduler.WeekDays.Sunday | DevExpress.XtraScheduler.WeekDays.Monday
                  | DevExpress.XtraScheduler.WeekDays.Tuesday | DevExpress.XtraScheduler.WeekDays.Wednesday
                  | DevExpress.XtraScheduler.WeekDays.Thursday);
            GenerateHolidaysFor2015();
            schedulerControl1.EndUpdate();
            this.schedulerControl1.ActiveViewType = SchedulerViewType.Day;
            this.dateNavigator1.HighlightHolidays = true;
            this.dateNavigator1.DateTime = new DateTime(2015, 02, 26);

        }

        // A collection of Kuwait Holidays for 2015.
        private Holiday[] KuwaitHolidays2015 = {
            new Holiday(new DateTime(2015, 01, 1), "New Year's Day"),
            new Holiday(new DateTime(2015, 01, 3), "The Prophet's Birthday"),
            new Holiday(new DateTime(2015, 02, 25), "National Day"),
            new Holiday(new DateTime(2015, 02, 26), "Liberation Day"),
            new Holiday(new DateTime(2015, 05, 16), "Isra and Miraj"),
            new Holiday(new DateTime(2015, 07, 18), "Eid al Fitr (End of Ramadan)"),
            new Holiday(new DateTime(2015, 07, 19), "Eid al Fitr holiday"),
            new Holiday(new DateTime(2015, 07, 20), "Eid al Fitr holiday"),
            new Holiday(new DateTime(2015, 09, 23), "Waqfat Arafat Day"),
            new Holiday(new DateTime(2015, 09, 24), "Eid al Adha (Feast of Sacrifice)"),
            new Holiday(new DateTime(2015, 09, 25), "Eid al Adha holiday"),
            new Holiday(new DateTime(2015, 09, 26), "Eid al Adha holiday"),
            new Holiday(new DateTime(2015, 10, 15), "Hejira New Year (Islamic New Year)"),
            new Holiday(new DateTime(2015, 12, 24), "The Prophet's Birthday"),
        };

        // This method adds holidays from the KuwaitHolidays2015 collection
        // to the WorkDays collection of the Scheduler Control.
        private void GenerateHolidaysFor2015() {
            foreach (Holiday item in KuwaitHolidays2015) {
                this.schedulerControl1.WorkDays.Add(item);
            }
        }

        private void schedulerControl1_CustomDrawDayHeader(object sender, CustomDrawObjectEventArgs e) {
            #region #CustomDrawDayHeader
            // Check whether the current object is a Day Header.
            SchedulerHeader header = e.ObjectInfo as SchedulerHeader;
            if (header != null) {
                // Check whether the current date is a known holiday.
                Holiday hol = FindHoliday(header.Interval.Start.Date);
                if (hol != null) {
                    header.Caption = hol.DisplayName;
                    e.DrawDefault();
                    // Add the holiday name to the day header's caption.
                    Image img = Image.FromFile("Kuwait.png");
                    Rectangle imgRect = header.ImageBounds;
                    imgRect.Width = header.ImageBounds.Height * img.Width / img.Height;
                    imgRect.X = header.ImageBounds.X + header.ImageBounds.Width - imgRect.Width;
                    e.Graphics.DrawImage(img, imgRect);
                    e.Handled = true;
                }
            }
            #endregion #CustomDrawDayHeader
        }

        Holiday FindHoliday(DateTime date) {
            WorkDaysCollection workDays = this.schedulerControl1.WorkDays;
            foreach (WorkDay item in workDays) {
                if (item is Holiday) {
                    Holiday hol = (Holiday)item;
                    if (hol.Date == date)
                        return hol;
                }
            }
            return null;
        }
    }
    #region #CustomCellStyleProvider
    public class CustomCellStyleProvider : ICalendarCellStyleProvider {
        DateNavigator dateNavigator;

        public CustomCellStyleProvider(DateNavigator calendar) {
            this.dateNavigator = calendar;
            myFont = new Font("Tahoma", 9);
            myImage = Image.FromFile("appointment_icon.png");
        }
        Font myFont;
        Image myImage;

        public void UpdateAppearance(CalendarCellStyle cell) {
            WorkDaysCollection workDays = this.dateNavigator.SchedulerControl.WorkDays;
            if (workDays.IsHoliday(cell.Date)) {
                switch (cell.State) {
                    // Highlight dates hovered over with the mouse.
                    case (DevExpress.Utils.Drawing.ObjectState.Hot):
                        cell.Appearance.ForeColor = Color.DarkGoldenrod;
                        cell.Appearance.BackColor = Color.PaleGreen;
                        break;
                    // Highlight selection.
                    case (DevExpress.Utils.Drawing.ObjectState.Selected):
                        cell.Appearance.ForeColor = Color.Gold;
                        cell.Appearance.BackColor = Color.Green;
                        break;
                    default:
                        cell.Appearance.ForeColor = Color.Green;
                        cell.Appearance.BackColor = Color.Gold;
                        break;
                }
            }
            // Display an image for the dates which contains appointments.
            if (cell.IsSpecial) {
                cell.Appearance.Font = myFont;
                cell.Image = myImage;
            }
        }
    }
    #endregion #CustomCellStyleProvider
}
vb
Imports DevExpress.Schedule
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.Drawing
Imports System
Imports System.Drawing

Namespace DisplayCustomHolidays
    Partial Public Class Form1
        Inherits DevExpress.XtraEditors.XtraForm

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            ' Specify a custom cell style provider to highlight cells that meet certain criteria.
            Me.dateNavigator1.CellStyleProvider = New CustomCellStyleProvider(Me.dateNavigator1)
            schedulerControl1.BeginUpdate()
            schedulerControl1.OptionsView.FirstDayOfWeek = FirstDayOfWeek.Sunday
            schedulerControl1.WorkDays.Clear()
            ' Specify working days of a week. Friday and Saturday are weekend holidays.
            schedulerControl1.WorkDays.Add(DevExpress.XtraScheduler.WeekDays.Sunday Or DevExpress.XtraScheduler.WeekDays.Monday Or DevExpress.XtraScheduler.WeekDays.Tuesday Or DevExpress.XtraScheduler.WeekDays.Wednesday Or DevExpress.XtraScheduler.WeekDays.Thursday)
            GenerateHolidaysFor2015()
            schedulerControl1.EndUpdate()
            Me.schedulerControl1.ActiveViewType = SchedulerViewType.Day
            Me.dateNavigator1.HighlightHolidays = True
            Me.dateNavigator1.DateTime = New Date(2015, 02, 26)

        End Sub

        ' A collection of Kuwait Holidays for 2015.
        Private KuwaitHolidays2015() As Holiday = { _
            New Holiday(New Date(2015, 01, 1), "New Year's Day"), _
            New Holiday(New Date(2015, 01, 3), "The Prophet's Birthday"), _
            New Holiday(New Date(2015, 02, 25), "National Day"), _
            New Holiday(New Date(2015, 02, 26), "Liberation Day"), _
            New Holiday(New Date(2015, 05, 16), "Isra and Miraj"), _
            New Holiday(New Date(2015, 07, 18), "Eid al Fitr (End of Ramadan)"), _
            New Holiday(New Date(2015, 07, 19), "Eid al Fitr holiday"), _
            New Holiday(New Date(2015, 07, 20), "Eid al Fitr holiday"), _
            New Holiday(New Date(2015, 09, 23), "Waqfat Arafat Day"), _
            New Holiday(New Date(2015, 09, 24), "Eid al Adha (Feast of Sacrifice)"), _
            New Holiday(New Date(2015, 09, 25), "Eid al Adha holiday"), _
            New Holiday(New Date(2015, 09, 26), "Eid al Adha holiday"), _
            New Holiday(New Date(2015, 10, 15), "Hejira New Year (Islamic New Year)"), _
            New Holiday(New Date(2015, 12, 24), "The Prophet's Birthday") _
        }

        ' This method adds holidays from the KuwaitHolidays2015 collection
        ' to the WorkDays collection of the Scheduler Control.
        Private Sub GenerateHolidaysFor2015()
            For Each item As Holiday In KuwaitHolidays2015
                Me.schedulerControl1.WorkDays.Add(item)
            Next item
        End Sub

        Private Sub schedulerControl1_CustomDrawDayHeader(ByVal sender As Object, ByVal e As CustomDrawObjectEventArgs) Handles schedulerControl1.CustomDrawDayHeader
' #Region "#CustomDrawDayHeader"
            ' Check whether the current object is a Day Header.
            Dim header As SchedulerHeader = TryCast(e.ObjectInfo, SchedulerHeader)
            If header IsNot Nothing Then
                ' Check whether the current date is a known holiday.
                Dim hol As Holiday = FindHoliday(header.Interval.Start.Date)
                If hol IsNot Nothing Then
                    header.Caption = hol.DisplayName
                    e.DrawDefault()
                    ' Add the holiday name to the day header's caption.
                    Dim img As Image = Image.FromFile("Kuwait.png")
                    Dim imgRect As Rectangle = header.ImageBounds
                    imgRect.Width = header.ImageBounds.Height * img.Width \ img.Height
                    imgRect.X = header.ImageBounds.X + header.ImageBounds.Width - imgRect.Width
                    e.Graphics.DrawImage(img, imgRect)
                    e.Handled = True
                End If
            End If
' #End Region ' #CustomDrawDayHeader
        End Sub

        Private Function FindHoliday(ByVal [date] As Date) As Holiday
            Dim workDays As WorkDaysCollection = Me.schedulerControl1.WorkDays
            For Each item As WorkDay In workDays
                If TypeOf item Is Holiday Then
                    Dim hol As Holiday = CType(item, Holiday)
                    If hol.Date = [date] Then
                        Return hol
                    End If
                End If
            Next item
            Return Nothing
        End Function
    End Class
    #Region "#CustomCellStyleProvider"
        Public Class CustomCellStyleProvider
        Implements ICalendarCellStyleProvider

        Private dateNavigator As DateNavigator
        Private myFont As Font
        Private myImage As Image

        Public Sub New(ByVal calendar As DateNavigator)
            Me.dateNavigator = calendar
            myFont = New Font("Tahoma", 9)
            myImage = Image.FromFile("appointment_icon.png")
        End Sub

        Public Sub UpdateAppearance(ByVal cell As CalendarCellStyle) Implements ICalendarCellStyleProvider.UpdateAppearance
            Dim workDays As WorkDaysCollection = Me.dateNavigator.SchedulerControl.WorkDays
            If workDays.IsHoliday(cell.Date) Then
                Select Case cell.State
                        ' Highlight dates hovered over with the mouse.
                    Case (DevExpress.Utils.Drawing.ObjectState.Hot)
                        cell.Appearance.ForeColor = Color.DarkGoldenrod
                        cell.Appearance.BackColor = Color.PaleGreen
                        ' Highlight selection.
                    Case (DevExpress.Utils.Drawing.ObjectState.Selected)
                        cell.Appearance.ForeColor = Color.Gold
                        cell.Appearance.BackColor = Color.Green
                    Case Else
                        cell.Appearance.ForeColor = Color.Green
                        cell.Appearance.BackColor = Color.Gold
                End Select
            End If
            ' Display an image for the dates which contains appointments.
            If cell.IsSpecial Then
                cell.Appearance.Font = myFont
                cell.Image = myImage
            End If
        End Sub
    End Class
    #End Region ' #CustomCellStyleProvider
End Namespace