Quantcast
Channel: Telerik Blogs
Viewing all articles
Browse latest Browse all 5210

Plan Your Holidays with Telerik UI for WinForms Scheduler

$
0
0

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 = newTelerik.WinControls.UI.Holiday();
holiday.Date = newDateTime(2018, 5, 24);
holiday.HolidayName = "Saints Cyril and Methodius Day";
holiday.Location = "Bulgaria";
boolgenerateAppointment = true;
this.radScheduler1.Holidays.AddHoliday(holiday, generateAppointment);

In the UI, it will be represented as an all-day appointment which is shown below:

SchedulerHolidays01

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: 

stringfileName = @".hol file location";
boolcreateAppointment = true;
this.radScheduler1.Holidays.ReadHolidays(fileName, createAppointment);

 SchedulerHolidays02

And here is how to load holidays for a specific country from the .hol file:

stringfileName = @".hol file location";
boolcreateAppointment = true;
this.radScheduler1.Holidays.ReadHolidays(fileName, createAppointment, newList<string>() { "Bulgaria"});

SchedulerHolidays03

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:

privatevoidradScheduler1_CellFormatting(objectsender, Telerik.WinControls.UI.SchedulerCellEventArgs e)
{
    if(!(e.CellElement isSchedulerHeaderCellElement))
    {
        if(this.radScheduler1.Holidays.IsHoliday(e.CellElement.Date))
        {
            strings = string.Empty;
            foreach(var item inthis.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);
        }
    }
}

SchedulerHolidays04

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:

privatevoidradScheduler1_AppointmentMoving(objectsender, 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!");
    }
}
 
privatevoidradScheduler1_AppointmentResizing(objectsender, 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!");
    }
}
 
privatevoidradScheduler1_AppointmentEditDialogShowing(objectsender, AppointmentEditDialogShowingEventArgs e)
{
    if(this.radScheduler1.Holidays.IsHoliday(e.Appointment.Start))
    {
        e.Cancel = true;
        RadMessageBox.Show("You can't add appointments for holidays!");
    }
}
 

SchedulerHolidays05

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.


Viewing all articles
Browse latest Browse all 5210

Trending Articles