articles

Home / DeveloperSection / Articles / DateTimePicker Control in C#.Net

DateTimePicker Control in C#.Net

Anonymous User27270 24-Jan-2011

A DateTimePicker control allows you to select a date and time, and to display that date and time in the specified format.

How to Use DateTimePicker Control

Drag and drop DateTimePicker control form toolbox on window Form.

DateTimePicker Control in C#.Net

Code:
using System;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    publicpartialclassForm7 : Form
    {
        public Form7()
        {
            InitializeComponent();
        }
        privatevoid dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            // show selected date
            label1.Text = dateTimePicker1.Text;
        }
    }
}
Run The Project

DateTimePicker Control in C#.Net

When you click on the calendar icon in the control then calendar popup will show.

DateTimePicker Control in C#.Net

When you select the date then ValueChanged event will fire and selected date will be show in Label.

DateTimePicker Control in C#.Net

DateTimePicker Properties:
Format: we can set DateTimePicker Format in which Format it should show.
Example:
privatevoid Form7_Load(object sender, EventArgs e)
{
        //Display the date in Short Format
        dateTimePicker1.Format = DateTimePickerFormat.Short;
}


DateTimePicker Control in C#.Net


Show checkbox:You can make a DateTimePicker control editable where you can change the value without using the popup Calendar. This can be done by setting ShowCheckBox property to true.

Example:
privatevoid Form7_Load(object sender, EventArgs e)
{
         // Makes DateTimePicker editable
         dateTimePicker1.ShowCheckBox = true;
}


DateTimePicker Control in C#.Net

Checkbox will show in DateTimePicker. You can edit the value in DateTimePicker and changed value will show in Label.

ShowUpDown:   We can also use an up-down control to set the date and time.

Example:
privatevoid Form7_Load(object sender, EventArgs e)
{
            //UpDown control will show in DateTimePicker
            dateTimePicker1.ShowUpDown = true;
}


DateTimePicker Control in C#.Net

UpDown Control   is display in DateTimePicker.we can change date through Updown button.

 


Updated 04-Mar-2020
I am a content writter !

Leave Comment

Comments

Liked By