corelibraries-devexpress-dot-xtrascheduler-dot-tools-dot-freetimecalculator.md
This event is raised for each interval before it is added to the collection. It enables you to change a free interval after it is found.
Namespace : DevExpress.XtraScheduler.Tools
Assembly : DevExpress.XtraScheduler.v25.2.Core.Desktop.dll
NuGet Package : DevExpress.Scheduler.CoreDesktop
public event IntervalFoundEventHandler IntervalFound
Public Event IntervalFound As IntervalFoundEventHandler
The IntervalFound event's data class is IntervalFoundEventArgs. The following properties provide information specific to this event:
| Property | Description |
|---|---|
| FreeIntervals | Gets or sets a collection, containing the discovered free interval. |
| Resource | Gets or sets a resource for appointments which are taken into account when a search for a free time is performed. |
This event gives you the flexibility to decide whether a particular free time slot satisfies your requirements.
The FreeIntervals property of IntervalFoundEventArgs object provides access to a TimeIntervalCollectionEx collection, which contains a single element - the located time interval. You can edit this interval by modifying its start, end and duration.
The following example illustrates how the FreeTimeCalculator.IntervalFound event can be used to find the spare time periods in work time only within the work week.
To accomplish this, subscribe to the event and then call the FreeTimeCalculator.FindFreeTimeInterval method.
using DevExpress.XtraScheduler;
using DevExpress.XtraScheduler.Tools;
// ...
private TimeInterval FindInterval(TimeInterval interval, TimeSpan duration) {
FreeTimeCalculator calculator =
new FreeTimeCalculator(schedulerControl1.Storage);
// Set a handler for the IntervalFound event.
calculator.IntervalFound += new IntervalFoundEventHandler(OnIntervalFound);
// Call the method which raises the event.
TimeInterval freeInterval = calculator.FindFreeTimeInterval(interval,
duration, true);
return freeInterval;
}
private void OnIntervalFound(object sender, IntervalFoundEventArgs args)
{ TimeIntervalCollectionEx freeIntervals = args.FreeIntervals;
DateTime start = freeIntervals.Start.Date.AddDays(-1);
DateTime end = freeIntervals.End;
while (start < end) {
RemoveSpareTime(freeIntervals, start);
RemoveNonworkingDays(freeIntervals, start);
start += TimeSpan.FromDays(1);
}
}
private void RemoveSpareTime(TimeIntervalCollectionEx freeIntervals, DateTime date) {
TimeInterval spareTime = new TimeInterval(date.AddHours(18),
TimeSpan.FromHours(15));
freeIntervals.Remove(spareTime);
}
private void RemoveNonworkingDays(TimeIntervalCollectionEx freeIntervals,
DateTime date) {
bool isWorkDay = schedulerControl1.WorkDays.IsWorkDay(date);
if (!isWorkDay)
freeIntervals.Remove(new TimeInterval(date, TimeSpan.FromDays(1)));
}
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.Tools
' ...
Private Function FindInterval(ByVal interval As TimeInterval, ByVal duration As TimeSpan) _
As TimeInterval
Dim calculator As FreeTimeCalculator = _
New FreeTimeCalculator(schedulerControl1.Storage)
' Set a handler for the IntervalFound event.
AddHandler calculator.IntervalFound, AddressOf OnIntervalFound
' Call the method which raises the event.
Dim freeInterval As TimeInterval = _
calculator.FindFreeTimeInterval(interval, duration, True)
Return freeInterval
End Function
Private Sub OnIntervalFound(ByVal sender As Object, _
ByVal args As IntervalFoundEventArgs)
Dim freeIntervals As TimeIntervalCollectionEx = args.FreeIntervals
Dim start As DateTime = freeIntervals.Start.Date.AddDays(-1)
Dim [end] As DateTime = freeIntervals.End
Do While start < [end]
RemoveSpareTime(freeIntervals, start)
RemoveNonworkingDays(freeIntervals, start)
start = start + TimeSpan.FromDays(1)
Loop
End Sub
Private Sub RemoveSpareTime(ByVal freeIntervals As TimeIntervalCollectionEx, _
ByVal [date] As DateTime)
Dim spareTime As TimeInterval = New TimeInterval([date].AddHours(18), _
TimeSpan.FromHours(15))
freeIntervals.Remove(spareTime)
End Sub
Private Sub RemoveNonworkingDays(ByVal freeIntervals As TimeIntervalCollectionEx, _
ByVal [date] As DateTime)
Dim isWorkDay As Boolean = schedulerControl1.WorkDays.IsWorkDay([date])
If (Not isWorkDay) Then
freeIntervals.Remove(New TimeInterval([date], TimeSpan.FromDays(1)))
End If
End Sub
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the IntervalFound 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-find-free-time-intervals/CS/FreeTimeIntervals/Form1.cs#L49
// Set a handler for the IntervalFound event.
calculator.IntervalFound += new IntervalFoundEventHandler(OnIntervalFound);
TimeInterval interval = TimeZoneHelper.FromClientTime(clientInterval);
winforms-scheduler-find-free-time-intervals/VB/FreeTimeIntervals/Form1.vb#L65
' Set a handler for the IntervalFound event.
AddHandler calculator.IntervalFound, New IntervalFoundEventHandler(AddressOf OnIntervalFound)
Dim interval As TimeInterval = TimeZoneHelper.FromClientTime(clientInterval)
See Also