When using RadScheduler for WinForms, it will almost always need to be customized in some way. This could come in the form of custom dialogs, context menus, or even custom appointments. In this blog entry, I am going to explain the steps required to add a custom field to RadScheduler.

Click here to download the source code and follow along...

Adding Custom Fields to Appointments

In this example, a custom field for Email will be added to the Appointment class being used by RadScheduler. The process of accomplishing this involves five simple steps.

Step 1: Add a Custom Field to the Data Source

In order to display a custom field in RadScheduler, the field will first need to already exist in, or be added to the data source. In this example, the database being used is based on the structure of the SchedulerData sample database included with the installation of RadControls for WinForms. Figure 1 shows the structure of the database. Note that a custom field for Email has been added to the Appointments table. This custom field is not available in the default Appointment class used by RadScheduler, so support for it will need to be added manually.
 
The Database Structure
Figure 1. The Database Structure

Step 2: Create a Custom Appointment Class

The Appointment class used by RadScheduler supports only a specific set of predefined fields by default. To add support for custom fields, this class needs to be extended. The easiest way to accomplish this is by simply creating a new class and inheriting from the Appointment class. The following code adds support for the custom Email field to the Appointment class.

public class AppointmentWithEmail : Appointment
{
    string _email = string.Empty;
 
    public AppointmentWithEmail()
        : base()
    {
    }
 
    public string Email
    {
        get { return _email; }
        set
        {
            if (_email != value) {
                _email = value;
                this.OnPropertyChanged("Email");
            }
        }
    }
}

Step 3: Create a Custom Appointment Factory

Appointments used in RadScheduler are created through the use of an IAppointmentFactory. By using a factory class, RadScheduler can easily generate and use appointments based on the IEvent interface regardless of their primary type. This includes custom appointments that inherit from the Appointment class. The following code implements a custom appointment factory based on the IAppointmentFactory interface.

public class CustomAppointmentFactory : IAppointmentFactory
{
    public IEvent CreateNewAppointment()
    {
        return new AppointmentWithEmail();
    }
}

After implementing this class, an instance of it will need to be assigned to the RadScheduler and its associated SchedulerBindingDataSource so it can begin using it to generate custom appointments. The following code should be placed in the OnLoad() method of the parent form.

this.radScheduler1.AppointmentFactory = new CustomAppointmentFactory();
schedulerBindingDataSource1.EventProvider.AppointmentFactory = radScheduler1.AppointmentFactory;

Step 4: Create and display a Custom Appointment Dialog

The Edit Appointment dialog was designed with the default Appointment class in mind and only displays information specific to its predefined fields. However, this dialog can easily be customized to display the custom Email field by following this series of quick steps.
  1. Right click the project containing the custom RadScheduler and select Add -> New Item...
  2. Select Windows Forms in the categories listing of the displayed dialog.
  3. Select Inherited Form and click Add.

    An "Inheritance Picker" dialog will display allowing a form to be chosen to inherit from.


  4. Click Browse...
  5. Browse to the installation folder of RadControls for WinForms.
  6. In the bin folder, select Telerik.WinControls.Scheduler.dll and click Open.

    The "Inheritance Picker" dialog will be populated with forms from the Telerik.WinControls.Scheduler.dll that can be inherited from.

  7. Select EditAppointmentDialog and click OK.

    This will generate a Form that inherits from the EditAppointmentDialog class.

  8. Add a new RadLabel and RadTextBox for the custom Email field to the designer.

    Custom Appointment Dialog
    Figure 2. The Custom Edit Appointment Dialog

  9. Right click the form in the designer and select View Code.
  10. Override the CreateNewEvent() method.

    This method is responsible for creating a new event. In this scenario, the CreateNewEvent() method will need to return a new instance of AppointmentWithEmail.
        
    protected override Telerik.WinControls.UI.IEvent CreateNewEvent()
    {
        return new AppointmentWithEmail();
    }

  11. Override the LoadSettingsFromEvent() method.

    This method is responsible for loading information into the user interface from source appointment. Since this dialog is customized for the AppointmentWithEmail object, the source can simply be cast to this type so the fields from it can be retrieved and displayed. 

    protected override void LoadSettingsFromEvent(Telerik.WinControls.UI.IEvent sourceEvent)
    {
        base.LoadSettingsFromEvent(sourceEvent);
     
        var appointmentWithEmail = sourceEvent as AppointmentWithEmail;
                 
        if (appointmentWithEmail != null)
            this.txtEmail.Text = appointmentWithEmail.Email;
    }
  12. Override the ApplySettingsToEvent() method.

    This method is responsible for applying information input into the user interface into the provided target appointment. Again, since the dialog is customized for the AppointmentWithEmail object, the target can simply be cast to this type.

    protected override void ApplySettingsToEvent(Telerik.WinControls.UI.IEvent targetEvent)
    {
        var appointmentWithEmail = targetEvent as AppointmentWithEmail;
                 
        if (appointmentWithEmail != null)
            appointmentWithEmail.Email = txtEmail.Text;
     
        base.ApplySettingsToEvent(targetEvent);
    }
After creating the custom EditAppointmentDialog, the AppointmentEditDialogShowing event of the RadScheduler must be subscribed to in order to show the new dialog. This event exists solely to allow developers to display a custom appointment dialog in place of the default. The following code provides the implementation for displaying the custom dialog.

private IEditAppointmentDialog appointmentDialog = null;
 
private void radScheduler1_AppointmentEditDialogShowing(object sender, Telerik.WinControls.UI.AppointmentEditDialogShowingEventArgs e)
{
    if (appointmentDialog == null)
        this.appointmentDialog = new AppointmentWithEmailEditForm();
 
    e.AppointmentEditDialog = this.appointmentDialog;
}

Step 5: Map the Custom Field to the Data Source

When using codeless data binding in RadScheduler, an instance of SchedulerBindingDataSource can easily be created through the Visual Studio designer. A dialog accessible through the Smart Tag of the SchedulerDataBindingSource allows developers to define mappings between the data source and the appointment class. Unfortunately, since this dialog is predefined, it has no way of knowing when an appointment with custom fields is being used. Due to this, the mapping between the data source and the appointment object must be defined manually through code. The following code should be placed in the OnLoad() method of the form containing RadScheduler.

AppointmentMappingInfo appointmentMappingInfo = (schedulerBindingDataSource1.EventProvider.Mapping as AppointmentMappingInfo);
appointmentMappingInfo.Mappings.Add(new SchedulerMapping("Email", "Email"));

Related Posts

Comments

Comments are disabled in preview mode.