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.
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
When you click on the calendar icon in the control then calendar popup will show.
When you select the date then ValueChanged event will fire and selected date will be show in Label.
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;
}
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;
}
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;
}
UpDown Control is display in DateTimePicker.we can change date through Updown button.
Anonymous User
01-Mar-2019Thank You for sharing it.
Sunil Singh
23-Jul-2017Thanks for sharing informative post.