Click here to Skip to main content
Licence 
First Posted 9 Dec 2002
Views 90,958
Bookmarked 69 times

A Time-Of-Day Picker control

By | 9 Dec 2002 | Article
A simple control that allows the user to pick time periods in the day

Sample Image - TimeFrame.jpg

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

License

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

ClearlyDotNet

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSmall Update PinmemberAngelo Cresta21:28 9 Jul '09  
GeneralAdd a vertical or horizontal scrollbar PinmemberJudyll4:24 11 Nov '07  
QuestionSample code VB.NET? Pinmemberpclady18:00 28 Nov '06  
Generalread graphical frame Pinmembercrisyuri12:47 2 Feb '06  
QuestionLoad Data Failed? Anyone can help me? Pinmemberxiaohu17:47 15 Jan '06  
GeneralLoading Data Pinmemberdietsche9:45 9 Apr '05  
QuestionHow to migrate to WebForm? PinmemberXTV22:27 27 Sep '04  
GeneralIt's genious! Pinmemberelmotheelk1:33 17 Aug '04  
GeneralTwo TImeFrame PinmemberJianhua Cao9:56 11 Aug '04  
GeneralRe: Two TImeFrame PinmemberJianhua10:04 11 Aug '04  
GeneralRe: Two TImeFrame Pinmemberfatcat111113:00 17 Feb '05  
GeneralRe: Two TImeFrame Pinmembertony1616160:38 12 Feb '06  
GeneralDid anyone manage to solve loading dataset into the control Pinmemberchrisybhot122:56 28 Apr '04  
Generalgetting this party started Pinmemberkvc3:36 25 Jul '03  
GeneralRe: getting this party started Pinmemberamun10023:37 26 Oct '09  
GeneralTimeSpans vs. DateTime PinmemberHeath Stewart8:12 20 Jan '03  
GeneralRe: TimeSpans vs. DateTime PinmemberClearlyDotNet7:40 30 Jan '03  
GeneralNice! PinmemberTim Hodgson4:33 16 Jan '03  
QuestionCool Control . but how to load data ? Pinmemberscryer210:03 17 Dec '02  
AnswerRe: Cool Control . but how to load data ? PinmemberClearlyDotNet7:44 30 Jan '03  
GeneralRe: Cool Control . but how to load data ? Pinmemberxavier217:02 3 Feb '03  
Generalbut how to load data ? Pinmembermaartensmolders1:21 7 Apr '03  
GeneralRe: but how to load data ? Pinmemberdl4gbe6:05 29 Nov '04  
GeneralRe: Cool Control . but how to load data ? Pinmembercmpenney6:09 13 Oct '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 10 Dec 2002
Article Copyright 2002 by ClearlyDotNet
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid