This tutorial shows how to create a custom schedule item class. For simplicity, the sample demonstrates that by inheriting a class from Appointment.
Follow steps 1 through 3 from Tutorial 1: Getting Started.
Select Project -> Add Class... from the menu, type the name of the custom item class in the dialog that appears (for example MyApp) and click 'OK'. Include the MindFusion.Scheduling namespace at the beginning of the file.
C# Copy Code |
---|
using MindFusion.Scheduling; |
Visual Basic Copy Code |
---|
Imports MindFusion.Scheduling |
The custom class will support a new property of type Boolean (called Kept) that will indicate whether the appointment has been kept or cancelled. The implementation of the property is trivial. Simply add a private field of type Boolean and make the property getter setter to return the field and to set the field respectively. The following sample code illustrates this:
C# Copy Code |
---|
public class MyApp : Appointment |
Visual Basic Copy Code |
---|
Class MyApp |
Note |
---|
You must provide constructor that takes no argument with your custom item class. MindFusion.Scheduling creates item class instances using reflection and an exception will be thrown if there isn't such constructor available. |
By default the Calendar control instantiates items of type Appointment when the user creates items interactively (by typing). To make the calendar create items of the new class, we need to add the following line of code in the form's Load event handler.
C# Copy Code |
---|
calendar.InteractiveItemType = typeof(MyApp); |
Visual Basic Copy Code |
---|
calendar.InteractiveItemType = GetType(MyApp) |
Now all items created by the user at runtime will be instances of MyApp.
To test whether the calendar actually creates MyApp instances, add an event handler to the control's ItemClick event. Add the following as a body of the event handler:
C# Copy Code |
---|
if (e.Item is MyApp) |
Visual Basic Copy Code |
---|
If TypeOf e.Item Is MyApp Then |
Clicking on an item now should display the message box.
Serialization of custom items requires additional efforts. Attempts to save a schedule containing items of type MyApp will result in an InvalidOperationException exception. In order to support serialization, we have to register MyApp with the schedule. For this purpose, add the following line of code somewhere in the form's Load event handler:
C# Copy Code |
---|
Schedule.RegisterItemClass(typeof(MyApp), "myapp", 1); |
Visual Basic Copy Code |
---|
Schedule.RegisterItemClass(GetType(MyApp), "myapp", 1) |
Now the exception won't be thrown, but we still need to override the SaveTo and LoadFrom methods of the Appointment class in order to serialize the custom property Kept. The code below illustrates how this is done for the SaveTo and LoadFrom overloads that serialize item's contents in binary streams.
C# Copy Code |
---|
public override void SaveTo(System.IO.BinaryWriter writer, BinarySerializationContext context) |
Visual Basic Copy Code |
---|
Public Overloads Overrides Sub SaveTo(ByVal writer As System.IO.BinaryWriter, ByVal context As BinarySerializationContext) |
A code that implements serialization of items in XML format would look similar to the above, but it will override the other SaveTo and LoadFrom method overloads.
Recurrence of items from a custom class is not handled wholly automatically. Let's take a closer look at how recurrence works in MindFusion.Scheduling. When a recurrence pattern is associated with an existing item, the item is said to become the master of the recurrence. Every time the occurrences of a master item must be displayed in a given time range, they are generated by instantiating new objects of the same type as that of the master. This is necessary in order to support infinite recurrences, where the generation of all recurrence instances is not possible. When the occurrences are generated, their properties are copied from the master item. This is done automatically for the built-in item properties, but not for those added by a derived item class. In order to copy the custom properties from the master item to a particular occurrence, you need to override the CopyOccurrence method. The following code sample shows how to do this.
C# Copy Code |
---|
protected override void CopyOccurrence(Item master) |
Visual Basic Copy Code |
---|
Protected Overloads Overrides Sub CopyOccurrence(ByVal master As Item) |
In addition, it is recommended to mark an item from a recurrent series as an exception when the value of a custom property changes. Otherwise, the change will be lost the next time the instance is generated.
C# Copy Code |
---|
//... |
Visual Basic Copy Code |
---|
' ... |
Interactive item cloning has been introduced in version 4.0. The users can clone items by holding down a particular key (the SHIFT key by default). To enable your custom items for interactive cloning, you have to override the Clone method, create a new object from your custom type and duplicate the fields of the prototype item. If you do not override the Clone method, MindFusion.Scheduling will still create copies of the item, but they will be of type Appointment instead of the custom item type. Here is a sample implementation of the Clone method.
C# Copy Code |
---|
//... |
Visual Basic Copy Code |
---|
'... |
In order to enable undo/redo of changes done on MyApp, create an ItemState-derived class that contains member corresponding to the additional properties defined by MyApp:
C# Copy Code |
---|
public class MyAppState : ItemState |
Visual Basic Copy Code |
---|
Public Class MyAppState |
Now override the CreateState, SaveState and RestoreState methods in the custom appointment class:
C# Copy Code |
---|
protected override ItemState CreateState() |
Visual Basic Copy Code |
---|
Protected Overloads Function CreateState() As ItemState Protected Overloads Function SaveState() As ItemState |