Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WinForms .NET Time Chooser Control

0.00/5 (No votes)
14 Oct 2014 1  
WinForms .NET Time Chooser grid control.

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

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:

//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:

//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.

//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:

//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:

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 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