C# DateTimePicker Control

The DateTimePicker control allows you to display and collect date and time from the user with a specified format.

C# datetimepicker

The DateTimePicker control has two parts, a label that displays the selected date and a popup calendar that allows users to select a new date. The most important property of the DateTimePicker is the Value property, which holds the selected date and time.

  dateTimePicker1.Value = DateTime.Today;

The Value property contains the current date and time the control is set to. You can use the Text property or the appropriate member of Value to get the date and time value.

  DateTime iDate;

iDate = dateTimePicker1.Value;

The control can display one of several styles, depending on its property values. The values can be displayed in four formats, which are set by the Format property: Long, Short, Time, or Custom.

  dateTimePicker1.Format = DateTimePickerFormat.Short;

The following C# program shows how to set and get the value of a DateTimePicker1 control.




using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Format = DateTimePickerFormat.Short;
            dateTimePicker1.Value = DateTime.Today;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime iDate;
            iDate = dateTimePicker1.Value;
            MessageBox.Show("Selected date is " + iDate);
        }
    }
}

 C# GUI - Related Contents



.