Introduction
This control uses double buffering to provide an interface that allows a user
to choose time frames throughout the day. The control is modeled after a control
that originally showed up in Windows NT, which allowed a user to specify valid
login hours to the domain.
About the control
The control has a property called TimeFrames
that
returns a 3 dimensional array of TimeSpan
objects.
public TimeSpan[][][] TimeFrames
- The first dimension is the day
- The second dimension is a specific contiguous time span
- The third dimension will only have two elements, the first is the start time
and the second is the end time
As suggested in the Eric White & Chris Garrett text "GDI+ Programming:
Creating Custom Controls Using C#", all sections of the screen are split up into
seperate objects. Since all major areas of the control can be hot tracked or
selected, there is a base concept of a TimeFrameObject
,
which all sub areas inherit from. This base class includes members for the
bounds of the object and whether the object is hot tracked or selected. This
really cuts down on the code. For example, this method figures out where on the
screen a particular point is and figures out which TimeFrameObject
contains the point.
private TimeFrameObject GetTimeFrameObject( MouseEventArgs e )
{
TimeFrameObject tfo = null;
if( Grid.Contains( e.X, e.Y ) )
{
tfo = CellMatrix[ (int)( ( e.Y - Header ) / y_grid_inc),
(int)( ( e.X - LeftMargin ) / x_grid_inc) ];
}
else if( ( e.X > Grid.X ) && ( e.X <
( Grid.X + Grid.Width ) ) && ( e.Y < Grid.Y ) )
{
tfo = (TimeFrameObject)
TimeSlots[ (int)( ( e.X - LeftMargin ) / x_grid_inc) ];
}
else if( ( e.X < Grid.X ) &&
( e.Y < ( Grid.Y + Grid.Height ) ) && ( e.Y > Grid.Y ) )
{
tfo = (_Day)Days[ (int)( ( e.Y - Header ) / y_grid_inc) ];
}
else if( TopLeft.Contains( e.X, e.Y ) )
{
tfo = TopLeft;
}
return tfo;
}
This reduces OnMouseDown
to:
protected override void OnMouseDown( MouseEventArgs e )
{
TimeFrameObject tfo = GetTimeFrameObject( e );
if( tfo != null )
{
if( e.Button == MouseButtons.Left )
{
tfo.Selected = ! tfo.Selected;
tfoLastSelected = tfo;
this.Invalidate( true );
}
}
}
That's all