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

DateTimePicker Control in C#.Net


C# C# 
Ratings:
0 Comment(s)
 18186  View(s)
Rate this:

DateTimePicker Control in C#.Net


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

{

    public partial class Form7 : Form

    {

        public Form7()

        {

            InitializeComponent();

        }

        private void 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:

private void 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:

private void 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:

private void 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.

 


Don't want to miss updates? Please click the below button!

Follow MindStick