TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# DateTimePicker Example

This C# tutorial uses the DateTimePicker control in Windows Forms.

DateTimePicker presents an interface for date selection.

It allows the user to select a valid DateTime without much effort on your part. We review this control and its associated options and properties here.

Example. This example gets us started with the DateTimePicker control. Please add the DateTimePicker control to your Form, and then add the ValueChanged event handler by right-clicking on the DateTimePicker, and going to Properties.

Then: Please click the lightning bolt. Add the Form1_Load event handler by double-clicking on the Form.

C# program that uses DateTimePicker

using System;
using System.Windows.Forms;

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

	private void Form1_Load(object sender, EventArgs e)
	{
	    // Initialize picker to yesterday.
	    DateTime result = DateTime.Today.Subtract(TimeSpan.FromDays(1));
	    dateTimePicker1.Value = result;
	}

	private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
	{
	    // Set title bar to selected date.
	    DateTime result = dateTimePicker1.Value;
	    this.Text = result.ToString();
	}
    }
}

Result

    1. When program starts, DateTimePicker shows yesterday.
    2. When you change the date, it is shown in the title bar.

Form1_Load. This is the Load event handler for the enclosing Form. First, we get the DateTime value for yesterday. This is done with Today, Subtract, and TimeSpan. The Value property is assigned to the resulting DateTime.

DateTime ExamplesDateTime.TodayDateTime Subtract MethodTimeSpan Examples

 

ValueChanged is often useful. Whenever the user changes the DateTime, this event handler is executed. In the method body, we assign the window's title text to the string representation of the DateTime that was selected.

ShowCheckBox. The ShowCheckBox property changes the DateTimePicker to display a check box on the left side. The user can enable or disable the DateTimePicker this way. To see if the check box is checked, use the Checked property on the DateTimePicker.

ShowUpDown. This control also shows the ShowUpDown property set to True. The ShowUpDown property changes the DateTimePicker such that it displays an up and down arrow box on the right side instead of the drop-down menu.

Tip: It is possibly a good idea to set ShowUpDown to True when the DateTime will only need to be changed up or down by a couple units.

Summary. The DateTimePicker control can be used with the ValueChanged event handler. You can change the ShowCheckBox and ShowUpDown properties to change how the DateTimePicker is displayed. The Value property sets or gets and the selected DateTime.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf