Skip to content
Related Articles

Related Articles

C# | DateTimePicker Class
  • Difficulty Level : Basic
  • Last Updated : 05 Sep, 2019

In Windows Forms, the DateTimePicker control is used to select and display the date/time with a specific format in your form. The FlowLayoutPanel class is used to represent windows DateTimePicker control and also provide different types of properties, methods, and events. It is defined under System.Windows.Forms namespace. You can create two different types of DateTimePicker, as a drop-down list with a date represented in the text, or as a calendar which appears when you click on the down-arrow next to the given list. In C#, you can create a DateTimePicker in the windows form by using two different ways:

1. Design-Time: It is the easiest way to create a DateTimePicker control as shown in the following steps:

  • Step 1: Create a windows form as shown in the below image:
    Visual Studio -> File -> New -> Project -> WindowsFormApp

  • Step 2: Next, drag and drop the DateTimePicker control from the toolbox to the form as shown in the below image:

  • Step 3: After drag and drop you will go to the properties of the DateTimePicker to modify DateTimePicker according to your requirement.

    Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can create a DateTimePicker programmatically with the help of syntax provided by the DateTimePicker class. The following steps show how to set the create DateTimePicker dynamically:



  • Step 1: Create a DateTimePicker using the DateTimePicker() constructor is provided by the DateTimePicker class.
    // Creating a DateTimePicker
    DateTimePicker d = new DateTimePicker();
    
  • Step 2: After creating a DateTimePicker, set the properties of the DateTimePicker provided by the DateTimePicker class.
    // Setting the location of the DateTimePicker
    d.Location = new Point(360, 162); 
    
    // Setting the size of the DateTimePicker
    d.Size = new Size(292, 26); 
    
    // Setting the maximum date of the DateTimePicker
    d.MaxDate = new DateTime(2500, 12, 20); 
    
    // Setting the minimum date of the DateTimePicker
    d.MinDate = new DateTime(1753, 1, 1); 
    
    // Setting the format of the DateTimePicker
    d.Format = DateTimePickerFormat.Long; 
    
    // Setting the name of the DateTimePicker
    d.Name = "MyPicker"; 
    
    // Setting the font of the DateTimePicker
    d.Font = new Font("Comic Sans MS", 12);
    
    // Setting the visibility of the DateTimePicker 
    d.Visible = true; 
    
    // Setting the value of the DateTimePicker
    d.Value = DateTime.Today; 
    
  • Step 3: And last add this DateTimePicker control to the form and also add other controls on the DateTimePicker using the following statements:
    // Adding this control 
    // to the form 
    this.Controls.Add(d); 
    

    Example:




    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp48 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating and setting the
            // properties of the Label
            Label l = new Label();
            l.Location = new Point(183, 162);
            l.Size = new Size(172, 20);
            l.Text = "Select Date and Time";
            l.Font = new Font("Comic Sans MS", 12);
      
            // Adding this control
            // to the form
            this.Controls.Add(l);
      
            // Creating and setting the
            // properties of the DateTimePicker
            DateTimePicker d = new DateTimePicker();
            d.Location = new Point(360, 162);
            d.Size = new Size(292, 26);
            d.MaxDate = new DateTime(2500, 12, 20);
            d.MinDate = new DateTime(1753, 1, 1);
            d.Format = DateTimePickerFormat.Long;
            d.Name = "MyPicker";
            d.Font = new Font("Comic Sans MS", 12);
            d.Visible = true;
            d.Value = DateTime.Today;
      
            // Adding this control
            // to the form
            this.Controls.Add(d);
        }
    }
    }

    Output:

Constructor

ConstructorDescription
DateTimePicker()This constructor is used to initializes a new instance of the DateTimePicker class.

Fields

FieldsDescription
DefaultMonthBackColorThis is field specifies the default month background color of the DateTimePicker control. This field is read-only.
DefaultTitleBackColorThis is field specifies the default title back color of the DateTimePicker control. This field is read-only.
DefaultTitleForeColorThis is field specifies the default title foreground color of the DateTimePicker control. This field is read-only.
DefaultTrailingForeColorThis is field specifies the default trailing foreground color of the DateTimePicker control. This field is read-only.
MaxDateTimeThis is field specifies the maximum date value of the DateTimePicker control. This field is read-only.
MinDateTimeThis is field get the minimum date value of the DateTimePicker control.

Properties

PropertyDescription
AutoSizeThis property is used to get or set a value that indicates whether the control resizes based on its contents.
AutoSizeModeThis property indicates the automatic sizing behavior of the control.
BackColorThis property is used to get or set the background color for the control.
BorderStyleThis property indicates the border style for the control.
CalendarFontThis property is used to get or set the font style applied to the calendar.
CalendarForeColorThis property is used to get or set the foreground color of the calendar.
CalendarMonthBackgroundThis property is used to get or set the background color of the calendar month.
CalendarTitleBackColorThis property is used to get or set the background color of the calendar title.
CalendarTitleForeColorThis property is used to get or set the foreground color of the calendar title.
CalendarTrailingForeColorThis property is used to get or set the foreground color of the calendar trailing dates.
FontThis property is used to get or set the font of the text displayed by the control.
ForeColorThis property is used to get or set the foreground color of the control.
FormatThis property is used to get or set the format of the date and time displayed in the control.
HeightThis property is used to get or set the height of the control.
LocationThis property is used to get or set the coordinates of the upper-left corner of the DateTimePicker control relative to the upper-left corner of its form.
MaxDateThis property is used to get or set the maximum date and time that can be selected in the control.
MaximumDateTimeThis property is used to get the maximum date value allowed for the DateTimePicker control.
MinDateThis property is used to get or set the minimum date and time that can be selected in the control.
MinimumDateTimeThis property is used to set the minimum date value allowed for the DateTimePicker control.
NameThis property is used to get or set the name of the control.
ShowUpDownThis property is used to get or set a value indicating whether a spin button control (also known as an up-down control) is used to adjust the date/time value.
ShowCheckBoxThis property is used to get or set a value indicating whether a check box is displayed to the left of the selected date.
SizeThis property is used to get or set the height and width of the control.
VisibleThis property is used to get or set a value indicating whether the control and all its child controls are displayed.
ValueThis property is used to get or set the date/time value assigned to the control.
WidthThis property is used to get or set the width of the control.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :