using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
Dbi.WinControl.Schedule;
namespace
TimeBarCollectionCsharp
{
public partial
class Form1
: Form
{
//Declare the global variable for storing a
secondary handle to every TimeBar loaded/added in to the dbiSchedule control.
private System.Collections.Generic.List<dbiTimeBarItem>
globalTimeBarCollection = new
System.Collections.Generic.List<dbiTimeBarItem>();
public Form1()
{
InitializeComponent();
}
private void
DbiSchedule1_AfterTimeBarInserted(object
sender, AfterTimeBarInsertedEventArgs e)
{
//After a timebar is inserted in to the
control (through user interaction), it needs to be added to the global
collection.
globalTimeBarCollection.Add(e.TimeBarItem);
//This is done in the AfterTimeBarInserted
event, to ensure there is a proper reference to the timebar that now exists
inside the control.
}
private void
DbiSchedule1_BeforeTimeBarRemove(object sender,
BeforeTimeBarRemoveEventArgs e)
{
//Before the timebar is removed from the
control, remove it from the global collection.
globalTimeBarCollection.Remove(e.TimeBarItem);
//This is done in the BeforeTimeBarRemove
event, to ensure there is still a valid handle to the timebar object to remove
it from the
//global collection.
//If attempted from the AfterTimeBarRemove,
there would no longer be an actual reference to the object, and additional code
would need to
//be used to determine which timebar needs to
be removed from the global collection.
}
private void
DbiSchedule1_FirstDraw(object sender,
FirstDrawEventArgs e)
{
int cntItem;
dbiScheduleObject curSched =
new
dbiScheduleObject();
curSched.Start = new
DateTime(2013, 1, 1);
curSched.End = new
DateTime(2014, 1, 1);
curSched.TimeType = enumTimeType.Days;
curSched.DisplayTimeBarDates = true;
//The datetime pickers are synched with the
date range of the dbiScheduleObject being used.
this.DateTimePicker1.MinDate =
curSched.Start;
this.DateTimePicker1.MaxDate =
curSched.End;
this.DateTimePicker1.Value =
curSched.Start;
this.DateTimePicker2.MinDate =
curSched.Start;
this.DateTimePicker2.MaxDate =
curSched.End;
this.DateTimePicker2.Value =
curSched.End;
this.DbiSchedule1.Schedules.Add(curSched);
Random randGen;
randGen = new
Random(2360);
for (cntItem = 1;cntItem<=10;cntItem++)
{
dbiScheduleItem newItem =
new
dbiScheduleItem();
newItem.Text = "Test " +
cntItem.ToString();
dbiTimeBarItem newTimeBar =
new dbiTimeBarItem();
newTimeBar.Start = curSched.Start.AddDays(randGen.Next(6));
newTimeBar.End = newTimeBar.Start.AddDays(randGen.Next(6));
newItem.TimeBars.Add(newTimeBar);
//After a timebar is added to an internal
collection through code, it must also be added to the global collection.
globalTimeBarCollection.Add(newTimeBar);
newTimeBar = new
dbiTimeBarItem();
newTimeBar.Start = curSched.Start.AddDays(randGen.Next(6) + 12);
newTimeBar.End = newTimeBar.Start.AddDays(randGen.Next(6));
newItem.TimeBars.Add(newTimeBar);
//After a timebar is added to an internal
collection through code, it must also be added to the global collection.
globalTimeBarCollection.Add(newTimeBar);
this.DbiSchedule1.Items.Add(newItem);
}
DbiSchedule1.BarSelectBackColor =
SystemColors.Highlight;
}
private void
Button1_Click(object sender,
EventArgs e)
{
//Unselect all the currently selected timebars
in the control
foreach(dbiTimeBarItem
curSelTimeBar in this.DbiSchedule1.SelectedTimeBars())
{
curSelTimeBar.IsSelected = false;
}
int TotalTimeBarCount = 0;
//Loop through all the timebars stored in the
global collection
//if any of them intersect with the current
date range selected in the datetimepickers, they will be selected and counted
//After the loop finishes, the total amount of
timebars that intersect with the date range, will be displayed in the textbox.
foreach (dbiTimeBarItem
curTimeBar in globalTimeBarCollection)
{
bool inRange =
false;
if ((curTimeBar.Start.Date ==
DateTimePicker1.Value.Date) || (curTimeBar.End.Date ==
DateTimePicker2.Value.Date))
{
inRange = true;
}
if ((curTimeBar.Start.Date ==
DateTimePicker2.Value.Date) || (curTimeBar.End.Date ==
DateTimePicker1.Value.Date))
{
inRange = true;
}
if (curTimeBar.Start.Date <
DateTimePicker1.Value.Date)
{
if ((curTimeBar.End.Date >
DateTimePicker1.Value.Date) && (curTimeBar.End.Date <
DateTimePicker2.Value.Date))
{
inRange = true;
}
if (curTimeBar.End.Date >
DateTimePicker2.Value.Date)
{
inRange = true;
}
}
else
{
if ((DateTimePicker2.Value.Date >
curTimeBar.Start.Date) && (DateTimePicker2.Value.Date < curTimeBar.End.Date))
{
inRange = true;
}
if (DateTimePicker2.Value.Date >
curTimeBar.End.Date)
{
inRange = true;
}
}
//The TimeBar was determined to be within the
Date Range identified by the two DatePickers.
//The count of total TimeBars within the range
is incremented, and the TimeBar is selected.
if (inRange)
{
TotalTimeBarCount = TotalTimeBarCount + 1;
curTimeBar.IsSelected = true;
}
}
this.TextBox1.Text =
"TimeBars within Range = " +
TotalTimeBarCount.ToString();
}
}
}
|