Click here to Skip to main content
15,170,607 members
Articles / Desktop Programming / Windows Forms
Tip/Trick
Posted 14 Oct 2014

Stats

12.3K views
339 downloads
13 bookmarked

WinForms .NET Time Chooser Control

Rate me:
Please Sign up or sign in to vote.
4.38/5 (6 votes)
14 Oct 2014MIT3 min read
WinForms .NET Time Chooser grid control.

The software is provided "as is", "where is", "as available", without warranty or guarantee of any kind.

Image 1

Definitions

Data Code is a long string produced by the control to specify which Time Selection Blocks are in which state (Colour 1 or Colour 2). Date Code is made up of Time Selection Block code, which are separated by a commas.

Time Selection Block is a visual part of the control, e.g. the clickable rectangle for Tuesday, 10am is a Time Selection Block.

Time Selection Block code is contained within the Data Code and is produced for each Time Selection Block. It contains data about what state the Time Selection Block is in (Colour 1 or Colour 2).

Introduction

The Time Chooser controls was originally developed with the intention of allowing the end-user to choose when to block or allow certain operations, as there is no control that exists as part of the .NET Framework. However, now software developers can impliment the control into their own application, whether this be a security application, scheduled tasks or another.

At present, the .NET Framework only contains the 'DateTimePicker' control, which follows the form of a combo box. The Time Chooser control is in the form of a grid, which is displayed at all times (not in the form of a combo box or drop-down.

There are many types of applications that the Time Chooser could be used for, such as security applications (e.g. only at these hours do I want to allow access to this application, etc.) or a kiosk type application (e.g. only at these hours do I want to allow users to login, etc.).

Using the code

The control has two colours, Colour 1 and Colour 2, both of which are toggled between when clicking on a Time Selection Block and can be changed. The control stores each Time Selection Block in Time Selection Block code, in the format 'Day:Hour:Colour', each of which are separated by commas (,), apart from the last Time Selection Block.

The day is represented as a 'byte' from 0 to 6, with 0 being Monday and 6 being Sunday. The hour is represented as a 'byte' from 0 to 23, with 0 being 00:00 and 23 being 23:00. The colour is represented as a 'byte' from 1 to 2 with 1 being Colour 1 and 2 being Colour 2.

Example Data Code

0:0:1,0:1:1,0:2:1,0:3:1,0:4:1,0:5:1...

You can retreive the Data Code from the current time selection using the 'getData()' function:

C#
//getData() function

txtData.Text = timeChooser1.getData();

You can also set the status for all Time Selection Blocks by entering Data Code using the 'setStatusByData(...)' procedure:

C#
//setStatusByData(...) procedure

timeChooser1.setStatusByData(txtData.Text);

In order to check Data Code for a particular selection, you can use the 'System.String.Contains' function and check whether the Data Code contains the Time Selection Block code in the format mentioned previously.

C#
//getData() function

//Check whether Date Code contains a Time Selection Block in the format mentioned previously.
//Day:Hour:Colour, in this case, Tuesday:10am:Colour 2

if (timeChooser1.getData().Contains("1:10:2")) //You could use Data Code from a stored value/file here
{
    //Perform code
}
else
{
    //Perform code
}

Finally, you can change the selection of a Time Selection Block using the 'setStatusByItem(...)' procedure along with the Time Selection Block code in the format mentioned previously:

C#
//setStatusByItem(...) function

//Set the status of a particular Time Selection Block by using the Time Selection Block code in the format mentioned previously.
//Day:Hour:Colour, in this case, Tuesday:10am:Colour 2

timeChooser1.setStatusByItem(1, 10, 2);

 

 

When a Time Selection Block is clicked, the code within the control toggles between the two colours as shown in the code:

C#
try
{
    if (panelX.BackColor == _colour1)
    {
        panelX.BackColor = _colour2;
    }
    else if (panelX.BackColor == _colour2)
    {
        panelX.BackColor = _colour1;
    }
}
catch { }

This is why Colour 1 cannot be the same as Colour 2.

 

Each Time Selection Block is a panel within the 'System.Windows.Forms' namespace

 

 

 

 

History

  • Version 1.0.0.0 (Beta)
    • Set Status by Time Selection Block code (Day:Time:Colour)
    • Get Data Code, to store as a setting in a file
    • Set Status by Data Code

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Jamie Highfield
Systems Engineer Hut Six Security
United Kingdom United Kingdom
Applied Software Engineering student at Cardiff University and the National Software Academy. Software & Systems Engineer at Hut Six Security.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Alexander Sharykin16-Oct-14 0:16
MemberAlexander Sharykin16-Oct-14 0:16 
GeneralWell Done Pin
lewisstevens114-Oct-14 8:49
Memberlewisstevens114-Oct-14 8:49 

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.