Click here to Skip to main content
13,351,239 members (53,775 online)
Click here to Skip to main content
Add your own
alternative version


70 bookmarked
Posted 9 Dec 2002

A Time-Of-Day Picker control

, 9 Dec 2002
Rate this:
Please Sign up or sign in to vote.
A simple control that allows the user to pick time periods in the day

Sample Image - TimeFrame.jpg


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


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralSmall Update Pin
Angelo Cresta9-Jul-09 22:28
memberAngelo Cresta9-Jul-09 22:28 
GeneralAdd a vertical or horizontal scrollbar Pin
Judyll11-Nov-07 5:24
memberJudyll11-Nov-07 5:24 
QuestionSample code VB.NET? Pin
pclady28-Nov-06 19:00
memberpclady28-Nov-06 19:00 
Generalread graphical frame Pin
crisyuri2-Feb-06 13:47
membercrisyuri2-Feb-06 13:47 
QuestionLoad Data Failed? Anyone can help me? Pin
xiaohu15-Jan-06 18:47
memberxiaohu15-Jan-06 18:47 
GeneralLoading Data Pin
dietsche9-Apr-05 10:45
memberdietsche9-Apr-05 10:45 
QuestionHow to migrate to WebForm? Pin
XTV27-Sep-04 23:27
memberXTV27-Sep-04 23:27 
GeneralIt's genious! Pin
elmotheelk17-Aug-04 2:33
memberelmotheelk17-Aug-04 2:33 
GeneralTwo TImeFrame Pin
Jianhua Cao11-Aug-04 10:56
memberJianhua Cao11-Aug-04 10:56 
GeneralRe: Two TImeFrame Pin
Jianhua11-Aug-04 11:04
memberJianhua11-Aug-04 11:04 
GeneralRe: Two TImeFrame Pin
fatcat111117-Feb-05 14:00
memberfatcat111117-Feb-05 14:00 
GeneralRe: Two TImeFrame Pin
tony16161612-Feb-06 1:38
membertony16161612-Feb-06 1:38 
GeneralDid anyone manage to solve loading dataset into the control Pin
chrisybhot128-Apr-04 23:56
memberchrisybhot128-Apr-04 23:56 
Generalgetting this party started Pin
kvc25-Jul-03 4:36
memberkvc25-Jul-03 4:36 
GeneralRe: getting this party started Pin
amun10027-Oct-09 0:37
memberamun10027-Oct-09 0:37 
hi did u manage to find a solution to this?


GeneralTimeSpans vs. DateTime Pin
Heath Stewart20-Jan-03 9:12
memberHeath Stewart20-Jan-03 9:12 
GeneralRe: TimeSpans vs. DateTime Pin
ClearlyDotNet30-Jan-03 8:40
memberClearlyDotNet30-Jan-03 8:40 
GeneralNice! Pin
Tim Hodgson16-Jan-03 5:33
memberTim Hodgson16-Jan-03 5:33 
QuestionCool Control . but how to load data ? Pin
scryer2117-Dec-02 1:03
memberscryer2117-Dec-02 1:03 
AnswerRe: Cool Control . but how to load data ? Pin
ClearlyDotNet30-Jan-03 8:44
memberClearlyDotNet30-Jan-03 8:44 
GeneralRe: Cool Control . but how to load data ? Pin
xavier213-Feb-03 8:02
memberxavier213-Feb-03 8:02 
Generalbut how to load data ? Pin
maartensmolders7-Apr-03 2:21
membermaartensmolders7-Apr-03 2:21 
GeneralRe: but how to load data ? Pin
dl4gbe29-Nov-04 7:05
memberdl4gbe29-Nov-04 7:05 
GeneralRe: Cool Control . but how to load data ? Pin
cmpenney13-Oct-08 7:09
membercmpenney13-Oct-08 7:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 10 Dec 2002
Article Copyright 2002 by ClearlyDotNet
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid