Back to Devexpress

WorkDaysCollection Class

corelibraries-devexpress-dot-xtrascheduler-8ee8d59a.md

latest9.2 KB
Original Source

WorkDaysCollection Class

Represents a collection of dates, for which the information regarding a working activity is known.

Namespace : DevExpress.XtraScheduler

Assembly : DevExpress.XtraScheduler.v25.2.Core.Desktop.dll

NuGet Package : DevExpress.Scheduler.CoreDesktop

Declaration

csharp
public class WorkDaysCollection :
    NotificationCollection<WorkDay>,
    ICloneable
vb
Public Class WorkDaysCollection
    Inherits NotificationCollection(Of WorkDay)
    Implements ICloneable

The following members return WorkDaysCollection objects:

LibraryRelated API Members
Cross-Platform Class LibrarySchedulerPrintAdapter.GetWorkDays()
WorkDaysCollection.Clone()
WinForms ControlsSchedulerControl.WorkDays
SchedulerControlPrintAdapter.GetWorkDays()
ASP.NET Web Forms ControlsASPxScheduler.WorkDays
ASPxSchedulerStorageControl.WorkDays
ASP.NET MVC ExtensionsSchedulerSettings.WorkDays
SchedulerStorageControlSettings.WorkDays

Remarks

Populate the WorkDaysCollection collection with objects of WeekDaysWorkDay, Holiday and ExactWorkDay to feed the scheduler with information on work days and holidays. Use the WorkDaysCollection.IsWorkDay method to determine whether the specified date is a work day or a holiday.

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 DXCollectionBase<WorkDay> DXCollection<WorkDay> NotificationCollection<WorkDay> WorkDaysCollection

See Also

WorkDaysCollection Members

DevExpress.XtraScheduler Namespace