windowsforms-10866-controls-and-libraries-scheduler-examples-gantt-view-how-to-implement-dependency-constraints-in-the-gantt-view.md
This code snippet illustrates how to apply the AppointmentDependencyType.FinishToStart dependency restrictions on modifications of appointment Appointment.Start and Appointment.End values within the Gantt View.
The SchedulerOptionsCustomization.AllowAppointmentConflicts option of the Scheduler is set to AppointmentConflictsMode.Custom. It allows the SchedulerControl to raise the SchedulerControl.AllowAppointmentConflicts event.
The code within this event handler analyzes all dependencies of the specified (Finish-to-Start) type established with the appointment for which the event is raised. If Start or End properties of the modified appointment do not meet criteria, it is thought that a conflict occurs.
If a conflict occurs, appointment modifications are canceled automatically.
private void schedulerControl1_AllowAppointmentConflicts(object sender, AppointmentConflictEventArgs e)
{
e.Conflicts.Clear();
AppointmentDependencyBaseCollection depCollectionDep =
schedulerStorage1.AppointmentDependencies.Items.GetDependenciesByDependentId(e.Appointment.Id);
if (depCollectionDep.Count > 0) {
if (CheckForInvalidDependenciesAsDependent(depCollectionDep, e.AppointmentClone))
e.Conflicts.Add(e.AppointmentClone);
}
AppointmentDependencyBaseCollection depCollectionPar =
schedulerStorage1.AppointmentDependencies.Items.GetDependenciesByParentId(e.Appointment.Id);
if (depCollectionPar.Count > 0) {
if (CheckForInvalidDependenciesAsParent(depCollectionPar, e.AppointmentClone))
e.Conflicts.Add(e.AppointmentClone);
}
}
private bool CheckForInvalidDependenciesAsDependent(AppointmentDependencyBaseCollection depCollection, Appointment apt)
{
foreach (AppointmentDependency dep in depCollection) {
if (dep.Type == AppointmentDependencyType.FinishToStart) {
DateTime checkTime = schedulerStorage1.Appointments.Items.GetAppointmentById(dep.ParentId).End;
if (apt.Start < checkTime)
return true;
}
}
return false;
}
private bool CheckForInvalidDependenciesAsParent(AppointmentDependencyBaseCollection depCollection, Appointment apt)
{
foreach (AppointmentDependency dep in depCollection) {
if (dep.Type == AppointmentDependencyType.FinishToStart) {
DateTime checkTime = schedulerStorage1.Appointments.Items.GetAppointmentById(dep.DependentId).Start;
if (apt.End > checkTime)
return true;
}
}
return false;
}
Private Sub schedulerControl1_AllowAppointmentConflicts(ByVal sender As Object, ByVal e As AppointmentConflictEventArgs)
e.Conflicts.Clear()
Dim depCollectionDep As AppointmentDependencyBaseCollection = schedulerStorage1.AppointmentDependencies.Items.GetDependenciesByDependentId(e.Appointment.Id)
If depCollectionDep.Count > 0 Then
If CheckForInvalidDependenciesAsDependent(depCollectionDep, e.AppointmentClone) Then
e.Conflicts.Add(e.AppointmentClone)
End If
End If
Dim depCollectionPar As AppointmentDependencyBaseCollection = schedulerStorage1.AppointmentDependencies.Items.GetDependenciesByParentId(e.Appointment.Id)
If depCollectionPar.Count > 0 Then
If CheckForInvalidDependenciesAsParent(depCollectionPar, e.AppointmentClone) Then
e.Conflicts.Add(e.AppointmentClone)
End If
End If
End Sub
Private Function CheckForInvalidDependenciesAsDependent(ByVal depCollection As AppointmentDependencyBaseCollection, ByVal apt As Appointment) As Boolean
For Each dep As AppointmentDependency In depCollection
If dep.Type = AppointmentDependencyType.FinishToStart Then
Dim checkTime As DateTime = schedulerStorage1.Appointments.Items.GetAppointmentById(dep.ParentId).End
If apt.Start < checkTime Then
Return True
End If
End If
Next dep
Return False
End Function
Private Function CheckForInvalidDependenciesAsParent(ByVal depCollection As AppointmentDependencyBaseCollection, ByVal apt As Appointment) As Boolean
For Each dep As AppointmentDependency In depCollection
If dep.Type = AppointmentDependencyType.FinishToStart Then
Dim checkTime As DateTime = schedulerStorage1.Appointments.Items.GetAppointmentById(dep.DependentId).Start
If apt.End > checkTime Then
Return True
End If
End If
Next dep
Return False
End Function