In this blog post, you will learn more about the Scheduler control in Telerik UI for WinForms and how to use the new holiday support functionality that was added with the latest R2'18 Release.
The Scheduler (a.k.a. RadScheduler) in Telerik UI for WinForms is a highly customizable component for presenting a variety of schedules with appointments in different views such as Day, Week, Month and more. As of R2 2018, we have also introduced support for holidays in RadScheduler, where you are able to set specific dates as non-business days or block them from booking appointments.
Adding Holidays to the Scheduler Control
The Holidays collection stores all special days. It is publicly accessible and with the following API you can add holidays programmatically:
Telerik.WinControls.UI.Holiday holiday =
new
Telerik.WinControls.UI.Holiday();
holiday.Date =
new
DateTime(2018, 5, 24);
holiday.HolidayName =
"Saints Cyril and Methodius Day"
;
holiday.Location =
"Bulgaria"
;
bool
generateAppointment =
true
;
this
.radScheduler1.Holidays.AddHoliday(holiday, generateAppointment);
In the UI, it will be represented as an all-day appointment which is shown below:
Importing from an Outlook Holidays File
The .hol (Outlook Holiday) file extension was developed by Microsoft to store relevant non-working days in Outlook. We have added support for importing Outlook.hol files, so you can easily load holidays for different countries. Here's how you load a .hol file:
string
fileName = @
".hol file location"
;
bool
createAppointment =
true
;
this
.radScheduler1.Holidays.ReadHolidays(fileName, createAppointment);
And here is how to load holidays for a specific country from the .hol file:
string
fileName = @
".hol file location"
;
bool
createAppointment =
true
;
this
.radScheduler1.Holidays.ReadHolidays(fileName, createAppointment,
new
List<
string
>() {
"Bulgaria"
});
Highlighting Holidays
Once the Holidays collection is populated (either via the API or by importing a .hol file) we can take advantage of the formatting event to differentiate these dates. Here we have marked all days that contain US holidays with orange and the ones with Bulgarian holidays in green:
private
void
radScheduler1_CellFormatting(
object
sender, Telerik.WinControls.UI.SchedulerCellEventArgs e)
{
if
(!(e.CellElement
is
SchedulerHeaderCellElement))
{
if
(
this
.radScheduler1.Holidays.IsHoliday(e.CellElement.Date))
{
string
s =
string
.Empty;
foreach
(var item
in
this
.radScheduler1.Holidays.GetHolidays(e.CellElement.Date).OrderBy(ae => ae.HolidayName))
{
if
(!s.Contains(item.HolidayName))
s += item.HolidayName + Environment.NewLine;
}
e.CellElement.DrawText =
true
;
e.CellElement.Text = s.ToString();
e.CellElement.TextWrap =
true
;
e.CellElement.TextAlignment = ContentAlignment.BottomCenter;
Padding padding = e.CellElement.Padding;
if
(
this
.radScheduler1.ActiveViewType == SchedulerViewType.Month)
padding.Bottom = 22;
e.CellElement.Padding = padding;
if
(
this
.radScheduler1.Holidays.GetHolidays(e.CellElement.Date).Where(l => l.Location ==
"Bulgaria"
).Any())
e.CellElement.BackColor = Color.LightGreen;
else
e.CellElement.BackColor = Color.Orange;
}
else
{
e.CellElement.DrawText =
false
;
e.CellElement.ResetValue(RadItem.BackColorProperty, ValueResetFlags.Local);
}
}
}
Blocking Dates
In order to block the creation or editing of new appointments, you should remove the dialog for inserting a new appointment and disable the resizing or moving of an existing event to a holiday. To do this, cancel the AppointmentEditDialogShowing, AppointmentResizing and AppointmentMoving events for holidays:
private
void
radScheduler1_AppointmentMoving(
object
sender, AppointmentMovingEventArgs e)
{
if
(
this
.radScheduler1.Holidays.IsHoliday(e.NewDate) ||
this
.radScheduler1.Holidays.IsHoliday(e.NewDate.Add(e.Appointment.Duration)))
{
e.Cancel =
true
;
RadMessageBox.Show(
"You can't add appointments for holidays!"
);
}
}
private
void
radScheduler1_AppointmentResizing(
object
sender, AppointmentResizingEventArgs e)
{
if
(
this
.radScheduler1.Holidays.IsHoliday(e.NewStartDate) ||
this
.radScheduler1.Holidays.IsHoliday(e.NewStartDate.Add(e.NewDuration)))
{
e.Cancel =
true
;
RadMessageBox.Show(
"You can't add appointments for holidays!"
);
}
}
private
void
radScheduler1_AppointmentEditDialogShowing(
object
sender, AppointmentEditDialogShowingEventArgs e)
{
if
(
this
.radScheduler1.Holidays.IsHoliday(e.Appointment.Start))
{
e.Cancel =
true
;
RadMessageBox.Show(
"You can't add appointments for holidays!"
);
}
}
Try It Out and Share Your Feedback
RadScheduler is a part of the Telerik UI for WinForms suite, which you can learn more about via the product page and comes with a 30-day free trial giving you some time to explore the toolkit and consider using it for your current or upcoming WinForms development.
Lastly, we would love to hear what you think, so should you have any questions and/or comments, please share them to our Feedback Portal or in the comment section below.