Search
Tutorial 3: Using the Standard Forms

This tutorial shows how to use the standard MindFusion.Scheduling dialogs to edit appointments in the calendar. This tutorial extends the application created in the previous tutorial so if you haven't done it already, complete Tutorial 2: Creating Recurrent Items first.

1. Add a reference to the dialogs assembly

In order to use the stock forms that come with MindFusion.Scheduling, you must first add a reference to the assembly that contains them. To do so, open the Solution Explorer by selecting View -> Solution Explorer from the Visual Studio menu, then right-click on the 'References' item and select 'Add Reference' from the context menu. In the dialog that appears, navigate to the StandardForms.dll file and click 'OK'. The file is usually located in the 'c:\Program Files\MindFusion\MindFusion.Scheduling for WinForms' folder or in the folder where you have installed the product.

2. Handling the ItemClick event

The appointment form is usually displayed in response to double-clicking the appointment, so we will handle the ItemClick event of the Calendar control. Switch to design view, select the control and in the Properties window click the 'Events' button located in the toolbar near the top of the window. Navigate to the ItemClick event in the event list and double-click it. That automatically creates an event handler for the event (with default name calendar_ItemClick) and places the input cursor at its beginning.

3. Display the form

To display the form add the following code to the body of the event handler just created.

 Note

Be sure to include both MindFusion.Scheduling and MindFusion.Scheduling.WinForms namespace via the using keyword in the beginning of the file.

C#  Copy Code

// Display the form only if left mouse button is double-clicked
if (e.Button != MouseButtons.Left || e.Clicks != 2)
    return;

calendar.ResetDrag();

// Create the form
AppointmentForm form = new AppointmentForm(calendar.Schedule);
form.Text = e.Item.HeaderText;
form.SetAppointment(e.Item as Appointment);

// Display the form to the user
DialogResult result = form.ShowDialog(this);

if (result == DialogResult.Abort)
{
    // 'Abort' is returned when the user clicks the 'Delete' button.
    // It is up to us to delete the item from the schedule.
    if (e.Item.RecurrenceState == RecurrenceState.Occurrence ||
        e.Item.RecurrenceState == RecurrenceState.Exception)
        e.Item.Recurrence.MarkException(e.Item, true);
    else
        calendar.Schedule.Items.Remove(e.Item);
}

// Invalidate the control
calendar.Invalidate();

Visual Basic  Copy Code

' Display the form only if left mouse button is double-clicked
If e.Button <> MouseButtons.Left Or e.Clicks <> 2 Then
   Return
End If

calendar.ResetDrag()

' Create the form
Dim form As New AppointmentForm(calendar.Schedule)
form.Text = e.Item.HeaderText
form.SetAppointment(CType(e.Item, Appointment))

' Display the form to the user
Dim result As DialogResult = form.ShowDialog(this)

If result = DialogResult.Abort Then

    ' 'Abort' is returned when the user clicks the 'Delete' button.
    ' It is up to us to delete the item from the schedule.
    If e.Item.RecurrenceState = RecurrenceState.Occurrence Or _
        e.Item.RecurrenceState = RecurrenceState.Exception Then
        e.Item.Recurrence.MarkException(e.Item, True)
    Else
        calendar.Schedule.Items.Remove(e.Item)
    End If

End If

' Invalidate the control
calendar.Invalidate()

The form automatically updates the edited item with all modifications made by the user, except when the user clicks the 'Delete' button. In this situation the form doesn't do anything except that it returns DialogResult.Abort. It is a responsibility of the developer to remove the item from the schedule.

If the item is not a recurring one, we can simply remove it from the Items collection. If the item is a recurring item, then it is not within the collection and an attempt to remove it will result in exception. To mark a recurring item as deleted we invoke MarkException on the corresponding Recurrence object, passing to it a reference to the item as the first argument and true as the second argument.

4. Build and run

Compile and run the application. Create an appointment by typing and then double-click it to display the appointment dialog.