New to Telerik UI for WinForms? Download free 30-day trial

Custom Appointment Element

RadScheduler allows you to create and use your own custom appointment elements. Thus, you can add the desired elements to fit your business need. In the following example, we will add two LightVisualElements to display the time interval and description and one RadButtonElement to cancel the appointment.

Figure 1: Custom AppointmentElement

WinForms RadScheduler Custom AppointmentElement

1. Let's start with creating a derivative of the AppointmentElement class. We will add the desired elements in the CreateChildElements method. The Synchronize method is the appropriate place to display the correct data in the custom elements in the AppointmentElement. It is also necessary to override the DrawEventText method and leave it empty in order to prevent drawing the default text.


public class MyAppointmentElement : AppointmentElement
{
    public MyAppointmentElement(RadScheduler scheduler, SchedulerView view, IEvent appointment) : base(scheduler, view, appointment)
    {
    }

    StackLayoutElement panel = new StackLayoutElement();
    RadButtonElement button = new RadButtonElement();
    LightVisualElement timeInterval = new LightVisualElement();
    LightVisualElement description = new LightVisualElement();

    protected override void CreateChildElements()
    {
        base.CreateChildElements(); 

        panel.StretchHorizontally = true;
        panel.StretchVertically = true;
        panel.Orientation = Orientation.Vertical;
        panel.Children.Add(timeInterval);
        panel.Children.Add(description);
        panel.Children.Add(button);
        button.Click += button_Click;
        button.Text = "Cancel";
        panel.ShouldHandleMouseInput = false;
        panel.NotifyParentOnMouseInput = true;
        timeInterval.ShouldHandleMouseInput = false;
        timeInterval.NotifyParentOnMouseInput = true;
        description.ShouldHandleMouseInput = false;
        description.NotifyParentOnMouseInput = true;
        button.ShouldHandleMouseInput = true; 

        this.Children.Add(panel);
    }

    private void button_Click(object sender, EventArgs e)
    {
        this.Scheduler.Appointments.Remove(this.Appointment);
    }

    public override void Synchronize()
    {
        base.Synchronize(); 
        timeInterval.Text = this.Appointment.Start.ToLongTimeString() + " - " + this.Appointment.End.ToLongTimeString();
        description.Text = this.Appointment.Summary;
    }

    public override void DrawEventText(Telerik.WinControls.Paint.IGraphics graphics)
    {
        //leave the method empty to prevent the default appointment information to be drawn
    }
}

2. Next, it is necessary to replace the default AppointmentElement with the custom class. For this purpose, it is necessary to create a SchedulerElementProvider and override its CreateElement method:


public class MyElementProvider : SchedulerElementProvider
{
    public MyElementProvider(RadScheduler scheduler) : base(scheduler)
    {
    }

    protected override T CreateElement<T>(SchedulerView view, object context)
    {
        if (typeof(T) == typeof(AppointmentElement))
        {
            return new MyAppointmentElement(this.Scheduler, view, (IEvent)context)as T;
        }

        return base.CreateElement<T>(view, context);
    }
}

3. Last, you should set the RadScheduler.ElementProvider property to a new instance of the custom provider:


this.radScheduler1.ElementProvider = new MyElementProvider(this.radScheduler1);
In this article