Back to Devexpress

SchedulerControl.AllowAppointmentConflicts Event

windowsforms-devexpress-dot-xtrascheduler-dot-schedulercontrol-96b17fb8.md

latest13.8 KB
Original Source

SchedulerControl.AllowAppointmentConflicts Event

Occurs when the scheduler finds appointments that are in conflict, and the SchedulerOptionsCustomization.AllowAppointmentConflicts property is set to Custom.

Namespace : DevExpress.XtraScheduler

Assembly : DevExpress.XtraScheduler.v25.2.dll

NuGet Package : DevExpress.Win.Scheduler

Declaration

csharp
public event AppointmentConflictEventHandler AllowAppointmentConflicts
vb
Public Event AllowAppointmentConflicts As AppointmentConflictEventHandler

Event Data

The AllowAppointmentConflicts event's data class is AppointmentConflictEventArgs. The following properties provide information specific to this event:

PropertyDescription
AppointmentGets the appointment for which the event was raised. Inherited from AppointmentEventArgs.
AppointmentCloneGets the clone of the appointment being processed in the SchedulerControl.AllowAppointmentConflicts event.
ConflictsGets the collection of appointments which are considered to be conflicting with the current appointment.
IntervalGets the time interval which the event was raised for.

The event data class exposes the following methods:

MethodDescription
RemoveConflictsWithDifferentResource(AppointmentBaseCollection, Object)Removes all the conflicting appointments from the specified collection whose Appointment.ResourceId doesn’t match the specified Id.

Remarks

Handle the AllowAppointmentConflicts event to manually decide whether the current appointment conflicts with any other appointments. The appointment copy to check can be accessed via the AppointmentEventArgs.Appointment property.

Note

The AllowAppointmentConflicts event is fired only if the SchedulerOptionsCustomization.AllowAppointmentConflicts property is set to AppointmentConflictsMode.Custom.

How to Signalize a Conflict

This event’s arguments provide the AppointmentConflictEventArgs.Conflicts property, which returns the collection of appointments that are considered to be conflicting by the XtraScheduler. If the Conflicts collection is empty, this means that there are no conflicting appointments.

If you determine that the current appointment has no conflicts, you should clear the Conflicts collection in this event’s handler. If you determine that the current appointment has at least one conflict, then the Conflicts collection should contain at least one object that is different from the current appointment. In other words, the source appointment (accessed via the AppointmentEventArgs.Appointment property) or its copy isn’t considered to be conflicting if it’s present in the Conflicts collection. There should be other appointments in this collection to indicate the conflict.

The non-empty Conflicts collection cancels the user action that caused the conflict. Common practice is to clear the Conflicts collection at the beginning, and populate it with any appointment if the user’s action should be canceled. You can even create a fake appointment for this purpose, and add it to the collection.

What Properties to Analyze

The Appointment property of the AppointmentConflictEventArgs gets an appointment before modification.

The AppointmentConflictEventArgs.AppointmentClone property contains an appointment modified due to an end-user’s action. If an appointment is being edited using standard or customized dialogs, the Appointment property is an appointment before the end-user dialog is initiated. The AppointmentClone property gets an appointment being modified by means of the end-user dialog.

In a drag operation, the AppointmentClone is the appointment that intersects with existing ones. The Appointment property provides access to the current appointment before modification. So, when an appointment is dragged, its clone may conflict with the original appointment, and the Conflicts collection may contain one element - the original appointment.

This example handles the SchedulerControl.AllowAppointmentConflicts event to prevent movement of an appointment to a range where another appointment exists. The SchedulerOptionsCustomization.AllowAppointmentConflicts property is set to AppointmentConflictsMode.Custom to process appointment conflicts in a custom manner.

The code in the SchedulerControl.AllowAppointmentConflicts event handler checks whether the time interval of the modified appointment intersects with other appointments, including recurring series and exceptions. If such an appointment is found, it is added to the AppointmentConflictEventArgs.Conflicts collection. If the collection has at least one element, a conflict occurs and the Scheduler cancels changes.

To paint conflicts, the example handles the SchedulerControl.CustomDrawAppointmentBackground event. The AppointmentConflictsCalculator.CalculateConflicts method is called in this event handler to get the current conflicts.

View Example

csharp
schedulerControl1.OptionsCustomization.AllowAppointmentConflicts = AppointmentConflictsMode.Custom;
//...
private void SchedulerControl1_AllowAppointmentConflicts(object sender, AppointmentConflictEventArgs e) {
    e.Conflicts.Clear();
    FillConflictedAppointmentsCollection(e.Conflicts, e.Interval, ((SchedulerControl)sender).DataStorage.Appointments.Items, e.AppointmentClone);
}

void FillConflictedAppointmentsCollection(AppointmentBaseCollection conflicts, TimeInterval interval,
    AppointmentBaseCollection collection, Appointment currApt) {
    for(int i = 0; i < collection.Count; i++) {
        Appointment apt = collection[i];
        if(new TimeInterval(apt.Start, apt.End).IntersectsWith(interval) & !(apt.Start == interval.End || apt.End == interval.Start)) {
            if(apt.ResourceId == currApt.ResourceId) {
                conflicts.Add(apt);
            }
        }
        if(apt.Type == AppointmentType.Pattern) {
            FillConflictedAppointmentsCollection(conflicts, interval, apt.GetExceptions(), currApt);
        }
    }
}
vb
schedulerControl1.OptionsCustomization.AllowAppointmentConflicts = AppointmentConflictsMode.Custom
'...
Private Sub SchedulerControl1_AllowAppointmentConflicts(ByVal sender As Object, ByVal e As AppointmentConflictEventArgs)
    e.Conflicts.Clear()
    FillConflictedAppointmentsCollection(e.Conflicts, e.Interval, DirectCast(sender, SchedulerControl).DataStorage.Appointments.Items, e.AppointmentClone)
End Sub

Private Sub FillConflictedAppointmentsCollection(ByVal conflicts As AppointmentBaseCollection, ByVal interval As TimeInterval, ByVal collection As AppointmentBaseCollection, ByVal currApt As Appointment)
    For i As Integer = 0 To collection.Count - 1
        Dim apt As Appointment = collection(i)
        If (New TimeInterval(apt.Start, apt.End)).IntersectsWith(interval) And Not (apt.Start = interval.End OrElse apt.End = interval.Start) Then
            If apt.ResourceId = currApt.ResourceId Then
                conflicts.Add(apt)
            End If
        End If
        If apt.Type = AppointmentType.Pattern Then
            FillConflictedAppointmentsCollection(conflicts, interval, apt.GetExceptions(), currApt)
        End If
    Next i
End Sub

The appointment editing form in its OnOkButton method checks for appointment conflicts using the AppointmentFormControllerBase.IsConflictResolved method. If it returns false , the edited appointment cannot be saved and a proper message appears. This message text can be modified by the Localizer technique, i.e., overriding the Localizer.GetLocalizedString method in a SchedulerLocalizer descendant, and specifying the required text for the SchedulerStringId.Msg_Conflict identifier. Please review the Localizing WinForms Controls via Localizer Objects document, for more information.

The following code snippets (auto-collected from DevExpress Examples) contain references to the AllowAppointmentConflicts event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-scheduler-enforce-task-dependencies-gantt-view/CS/GanttRestrictions/Form1.cs#L28

csharp
this.schedulerControl1.OptionsCustomization.AllowAppointmentConflicts = AppointmentConflictsMode.Custom;
    this.schedulerControl1.AllowAppointmentConflicts+=new AppointmentConflictEventHandler(schedulerControl1_AllowAppointmentConflicts);
}

winforms-scheduler-resolve-appointment-conflicts/CS/DXApplication1/Form1.cs#L46

csharp
schedulerControl1.OptionsCustomization.AllowAppointmentConflicts = AppointmentConflictsMode.Custom;
schedulerControl1.AllowAppointmentConflicts += SchedulerControl1_AllowAppointmentConflicts;
schedulerControl1.CustomDrawAppointmentBackground += SchedulerControl1_CustomDrawAppointmentBackground;

winforms-scheduler-enforce-task-dependencies-gantt-view/VB/GanttRestrictions/Form1.vb#L25

vb
schedulerControl1.OptionsCustomization.AllowAppointmentConflicts = AppointmentConflictsMode.Custom
    AddHandler schedulerControl1.AllowAppointmentConflicts, New AppointmentConflictEventHandler(AddressOf schedulerControl1_AllowAppointmentConflicts)
End Sub

winforms-scheduler-resolve-appointment-conflicts/VB/DXApplication1/Form1.vb#L37

vb
schedulerControl1.OptionsCustomization.AllowAppointmentConflicts = AppointmentConflictsMode.Custom
AddHandler schedulerControl1.AllowAppointmentConflicts, AddressOf SchedulerControl1_AllowAppointmentConflicts
AddHandler schedulerControl1.CustomDrawAppointmentBackground, AddressOf SchedulerControl1_CustomDrawAppointmentBackground

See Also

How to: Handle Appointment Conflicts

How to: Prevent End-Users from Editing Appointments

Localizing WinForms Controls with Localizer Objects

SchedulerControl Class

SchedulerControl Members

DevExpress.XtraScheduler Namespace