Date Controls And Timer Component In C#

Introduction

The DateTimePicker class is used to create the DateTimePicker control. The data members of this class change the appearance of the calendar, update styles, and track user-generated events.

DateTimePicker Control Properties

CalendarFont

Specifies or retrieves the font style applied to the calendar.

CalendarMonthBackground

Specifies or retrieves the background color for a particular month of the calendar.

CalendarTitleForeColor

Specifies or retrieves the forecolor for the title on the calendar.

Checked

Specifies or retrieves a value that indicates whether the value property is set to a valid date or time. It also indicates whether the displayed value can be updated.

Enabled

Specifies or retrieves a value that indicates whether the control responds to the user action.

ShowCheckBox

Specifies or retrieves a value, which indicates whether a check box is displayed towards the left side of the selected date.

Event of DateTimePicker Class

CloseUp

It occurs when the drop-down calendar disappears.

The following code demonstrates the use of some properties, methods, and events of the DateTimePicker class.

DateTimePicker dtpOrder=new DateTimePicker();
dtpOrder.ShowCheckBox=true;
dtpOrder.Checked=false;
dtpOrder.ShowUpDown=true;
dtpOrder.CalendarFont=new  Font(“Times New Roman”,18);
dtpOrder.Format=DateTimePickerFormat.Short;
private void dtporder_FormatChanged(object sender, EventArgs e)
{
    MessageBox.Show(“Format have been changed”,”DateTimePicker”);
}

In this code, an object of the DateTimePicker class is created namely, dtpOrder. The showCheckBox property is set to true. This means that a check box will be displayed on the left side of the control. The checked property is set to false, which means that the value in the control must be valid. The font of the calendar is set to Times New Roman with font size 18 by using the CalnedarFont property. The format of the date and time is set as Short. When this format is changed, the FormatChanged event is raised and the message box is displayed to the user.

CustomFormat Property

The CustomFormat property of the DateTimePicker class is used to set various custom format strings for displaying the date and time. The developer can also combine various format strings to display the Date/Time in the required format. The following code demonstrates the different format strings of the CustomFormat property.

DateTimePicker dtpOrder=new DateTimePicker();
DateTimePicker.dtpInvoice=new DateTimePicker();
dtpOrder.Format=DateTimePickerFormat.Custom;
dtpOrder.CustomFormat=”dd-MM-yyyy”;
dtpInvoice.Format=DateTimePickerFormat.Custom;
dtpInvoice.customFormat=”ddd, MM-dd-yyyy”;

In this code, two objects of the DateTimePicker class are created namely, dtpOrder and dtpInvoice. The format of the values for both the controls is set as Custom using the Format property. The CustomFormat property for the dtpOrder control specifies the custom format as dd-MM-yyyy. For the dtpInvoice control, the customFormat property is specified as ddd,MMM-dd-yyyy

MonthCalendar Control

The MonthCalendar class is used to create the MonthCalendar control. The data members of this class set or get the selected range of dates and identify the maximum and minimum dates.

The following demonstrates the properties, methods, and events of the MonthCalendar class.

MonthCalendar calPay=new MonthCalendar();
calPay.FirstDayOfWeek=Day.Monday;
calPay.MaxSelectionCount=15;
calPay.SetSelectionRange(DateTime.Today, DateTime.Parse(“10/27/21”));
MessageBox.Show(calPay.SelectionRange.ToString());

In this code, an object of the MonthCalendar class is created. The FirstDayOfWeek property is used to specify Wednesday as the first day of the week. This is done using the pay enumeration, which defines the days. The MaxSelectionCount property is used to specify the total number of dates that can be selected, which is 15. The SetSelectionRange() method is used to specify the range of dates that can be selected, which is from the present date to 27th October 2021. The SelectionRange property is used to retrieve the selected range and it is displayed in the message box.

Timer Component

The Timer class is used to create the Timer component. The properties of the Timer component identify whether the component is in its design mode. They also retrieve the list of event handlers attached with the component.

The following code demonstrates the use of properties of the Timer class.

Timer tmrCount = new Timer();
tmrCount.Enabled = true;
private void tmrCount_Tick(object sender, EventArgs e) {
    If(this.Text.Length > 0) {
        this.Text = ””;
    }
    else if (this.Text.Length == 0) {
        this.Text = ”Time is Over…”;
    }
}

In this code, an object of the Timer class is created, namely tmrCount. The Enabled property is set to true. By default, the value of the interval property is 100 milliseconds, Therefore, after every 100 milliseconds, the Tick event is raised. The length of the text on the title bar of the form is checked using the Text property of the form and this keyword. If it is greater than zero, the text disappears,, Now when the Tick event is raised again, the length of the text on the title bar of the form is 0. Then, the text “Time is Over” is displayed on the label on the form. This means that for every alternate Tick event, the text will appear on the label. This gives a blinking effect to the label.

Summary

The properties of the DateTimePicker class allow you to set the font of the title bar of the calendar and to display a check box in the control. Various formats can be specified for the date and time to be displayed in the DateTimePicker control. This is done using the CustomFormat property. The MaxDate and MinDate properties of the MonthCalendar class identify the maximum and minimum dates, respectively. The SetCalendarDimensions method of the MonthCalendar class sets the rows and columns to display multiple months. The property of the Timer class is used to indicate whether the component is currently in design mode or not.