Back to Devexpress

Holiday Class

corelibraries-devexpress-dot-schedule.md

latest7.6 KB
Original Source

Holiday Class

Represents a day that is recognized as a holiday.

Namespace : DevExpress.Schedule

Assembly : DevExpress.Data.v25.2.dll

NuGet Package : DevExpress.Data

Declaration

csharp
public class Holiday :
    KnownDateDay
vb
Public Class Holiday
    Inherits KnownDateDay

The following members return Holiday objects:

Remarks

The Holiday class establishes the specified date as a holiday. It provides a KnownDateDay.DisplayName property to name the holiday for display within a scheduler. This object’s instance should be added to the WorkDaysCollection of a scheduler control, available via the SchedulerControl.WorkDays property, for the holiday to be recognized.

Example

The following example demonstrates how to add custom holidays to your scheduling application. For this you need to create a Holiday object for every custom holiday, and add it to the SchedulerControl.WorkDays collection.

The code below demonstrates how to define a collection of custom holidays in the USA for 2007, and how to add them to the Scheduler Control.

csharp
using DevExpress.XtraScheduler;
using DevExpress.XtraScheduler.Drawing;
// ...

    // A collection of US Holidays for 2007.
    private Holiday[] USHolidays2007 = { 
        new Holiday(new DateTime(2007, 1, 1), "New Year's Day"),
        new Holiday(new DateTime(2007, 1, 15), "Martin Luther King Jr.'s Birthday"),
        new Holiday(new DateTime(2007, 2, 19), "Presidents' Day"),
        new Holiday(new DateTime(2007, 4, 6), "Good Friday"),
        new Holiday(new DateTime(2007, 5, 28), "Memorial Day"),
        new Holiday(new DateTime(2007, 7, 4), "Independence Day"),
        new Holiday(new DateTime(2007, 9, 3), "Labor Day"),
        new Holiday(new DateTime(2007, 11, 22), "Thanksgiving Day"),
        new Holiday(new DateTime(2007, 12, 25), "Christmas Day")
    };

    // This method adds all US Holidays from the USHolidays2007 collection
    // to the WorkDays collection of the Scheduler Control.
    private void GenerateHolidaysFor2007() {
        foreach (Holiday item in USHolidays2007) {
            this.schedulerControl1.WorkDays.Add(item);
        }
    }
vb
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.Drawing
' ...

    ' A collection of US Holidays for 2007.
    Private USHolidays2007 As Holiday() = { _
        New Holiday(New DateTime(2007, 1, 1), "New Year's Day"), _
        New Holiday(New DateTime(2007, 1, 15), "Martin Luther King Jr.'s Birthday"), _
        New Holiday(New DateTime(2007, 2, 19), "Presidents' Day"), _
        New Holiday(New DateTime(2007, 4, 6), "Good Friday"), _
        New Holiday(New DateTime(2007, 5, 28), "Memorial Day"), _
        New Holiday(New DateTime(2007, 7, 4), "Independence Day"), _
        New Holiday(New DateTime(2007, 9, 3), "Labor Day"), _
        New Holiday(New DateTime(2007, 11, 22), "Thanksgiving Day"), _
        New Holiday(New DateTime(2007, 12, 25), "Christmas Day") _
    }

    ' This method adds all US Holidays from the USHolidays2007 collection
    ' to the WorkDays collection of the Scheduler Control.
    Private Sub GenerateHolidaysFor2007()
        For Each item As Holiday In USHolidays2007
            Me.schedulerControl1.WorkDays.Add(item)
        Next item
    End Sub

For example, these holidays may be added when a form containing a Scheduler Control is loaded.

csharp
private void Form1_Load(object sender, EventArgs e) {
        GenerateHolidaysFor2007();
    }
vb
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
    Handles MyBase.Load
        GenerateHolidaysFor2007()
    End Sub

When a holiday is added to the SchedulerControl.WorkDays collection, the Date Navigator shows these holidays in Red (if its DateNavigator.HighlightHolidays property is set to true ). In addition, it may be required to mark holidays when they are displayed within a Scheduler Control. For example, you may handle the SchedulerControl.CustomDrawDayHeader event, and add the holiday’s name to the header’s caption, as shown in the code below.

csharp
private void schedulerControl1_CustomDrawDayHeader(object sender, 
CustomDrawObjectEventArgs e) {
    // 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) {

            // Add the holiday name to the day header's caption.
            header.Caption = String.Format("{0} ({1})", hol.DisplayName, 
                hol.Date.ToShortDateString());
        }
    }
}

// This method finds a holiday for the specified date.
private Holiday FindHoliday(DateTime date) {
    foreach (WorkDay item in schedulerControl1.WorkDays) {
        if (item is Holiday) {
            Holiday hol = (Holiday)item;
            if (hol.Date == date)
                return hol;
        }
    }
    return null;
}
vb
Private Sub schedulerControl1_CustomDrawDayHeader(ByVal sender As Object, _
    ByVal e As CustomDrawObjectEventArgs) Handles schedulerControl1.CustomDrawDayHeader
        ' Check whether the current object is a Day Header.
        Dim header As SchedulerHeader = TryCast(e.ObjectInfo, SchedulerHeader)
        If Not header Is Nothing Then

            ' Check whether the current date is a known holiday.
            Dim hol As Holiday = FindHoliday(header.Interval.Start.Date)
            If Not hol Is Nothing Then

                ' Add the holiday name to the day header's caption.
                header.Caption = String.Format("{0} ({1})", hol.DisplayName, _
                hol.Date.ToShortDateString())
            End If
        End If
    End Sub

    ' This method finds a holiday for the specified date.
    Private Function FindHoliday(ByVal [date] As DateTime) As Holiday
        For Each item As WorkDay In schedulerControl1.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

Inheritance

Object WorkDay KnownDateDay Holiday

See Also

Holiday Members

DevExpress.Schedule Namespace