DateTimePicker in C#.net

How to use DateTimePicker in C#.net

DateTimePicker in C#.net

The DateTimePicker control lets the user interactively pick a single date and time. It provides the developer lot of flexibility and assurance that any value it returns is a valid DateTime value, eliminating the need for extensive data validation. It also automatically considers the computer's regional settings when displaying various date and time formats.

The control uses a relatively small amount of screen real estate, being about the same size as a single line text box.

Once the calendar is dropped down, the user can change months by clicking on scroll buttons to either side of the month and year in the title or by clicking on the month name in the title and selecting from the drop-down menu. If the user clicks on the year in the title, up-down scroll buttons appear, which enable scrolling to a new year.

DateTimePicker properties

Property

Value type

Description

CalendarFont

Font

Read/write. The font applied to the calendar.

CalendarForeColor

Color

Read/write. The color of the text used for the calendar dates and the Today legend.

CalendarMonthBackground

Color

Read/write. The background color of the calendar month.

CalendarTitleBackColor

Color

Read/write. The background color of the calendar title.

CalendarTitleForeColor

Color

Read/write. The color of the text used for the calendar title and the selected date.

CalendarTrailingForeColor

Color

Read/write. The color of the text used for the calendar trailing dates, which precede and follow the current month in the calendar.

Checked

Boolean

Read/write. If true (the default), the Value property has been set with a valid value and the displayed value can be updated.

CustomFormat

String

Read/write. A string comprised of format strings ,  representing custom DateTime formats. Default is null (Nothing). Format property must be set to DateTimePickerFormat.Custom for this property to have any effect.

DropDownAlign

LeftRightAlignment

Read/write. Specifies the left/right alignment of the calendar. Default is LeftRightAlignment.Left. Other possible value is LeftRightAlignment.Right.

Format

DateTimePickerFormat

Read/write. Specifies the format for date and time. Must be a member of the DateTimePickerFormat enumeration, Actual displayed formats are determined by the user's operating system and regional settings. Default is DateTimePickerFormat.Long.

MaxDate

DateTime

Read/write. The maximum date and time that can be selected by the control. The default is 12/31/9998 23:59:59.

MinDate

DateTime

Read/write. The minimum date and time that can be selected by the control. The default is 1/1/1753 00:00:00.

PreferredHeight

Integer

Read-only. Minimum height of the control, in pixels, to accommodate the displayed text.

ShowCheckBox

Boolean

Read/write. If true, a checkbox is displayed to the left of the date-time value in the control. When this checkbox is checked, the values in the control can be changed by the user at runtime by highlighting a portion of the date and pressing the arrow keys. When not checked, the date can only be changed by clicking the drop-down menu to display the calendar. Default is false.

ShowUpDown

Boolean

Read/write. If true, the calendar drop-down menu is disabled and up-down scroll buttons are displayed to the right of the date-time value in the control, which are used to increment/decrement the highlighted portion of the date-time value. Default is false.

Text

String

Read/write. Overridden. Equivalent to the Value property with the appropriate or custom formatting applied.

Value

DateTime

Read/write. The DateTime value assigned to the control. If Value has not yet been set, it returns the current date and time (DateTime.Now).

 

DateTimePickerFormat enumeration

Value

Description

Custom

Date-time values displayed in a custom format

Long

Date-time values displayed in the long format set by the user's OS and regional settings

Short

Date-time values displayed in the short format set by the user's OS and regional settings

Time

Date-time values displayed in the time format set by the user's OS and regional settings

DateTimePicker events

Event

Event argument

Description

CloseUp

EventArgs

Raised when the drop-down calendar disappears

DropDown

EventArgs

Raised when the drop-down calendar is shown

FormatChanged

EventArgs

Raised when the Format property is changed

ValueChanged

EventArgs

Raised when the Value property changes

 

DateTimePicker in C# (DateTimePicker.cs)

using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace ProgrammingWinApps
{
   public class DTPicker : Form
   {
      DateTimePicker dtp;
      Label lblToString;
      Label lblLongDate;
      Label lblLongTime;
      Label lblShortDate;
      Label lblShortTime;
      Label lblCustomFormat;
      TextBox txtCustomString;
 
      public DTPicker(  )
      {
         Text = "DateTimePicker Demo";
         Size = new Size(400,300);
         this.Load += new EventHandler(this_Load);
 
         dtp = new DateTimePicker(  );
         dtp.Parent = this;
         dtp.Location = new Point(20,20);
         dtp.Size = new Size(ClientSize.Width - 40, dtp.PreferredHeight);
         dtp.Anchor = AnchorStyles.Top | AnchorStyles.Left | 
                  AnchorStyles.Right;
         Font fnt = new Font("Times New Roman", 16);
         dtp.CalendarFont = new Font(fnt, 
                                     FontStyle.Bold | FontStyle.Italic);
         dtp.CalendarForeColor = Color.Red;
         dtp.CalendarMonthBackground = Color.Yellow;
         dtp.CalendarTitleBackColor = Color.Lime;
         dtp.CalendarTitleForeColor = Color.Blue;
         dtp.CalendarTrailingForeColor = Color.FromArgb(255,192,192);
         dtp.CustomFormat = "dddd,MMMM d, yyyy 'at' h:mm:ss tt";
         dtp.Format = DateTimePickerFormat.Custom;
         dtp.DropDownAlign = LeftRightAlignment.Right;
         dtp.ShowUpDown = false;      // default
         dtp.ValueChanged += 
               new EventHandler(dtp_ValueChanged);
 
         Label lbl1 = new Label(  );
         lbl1.Parent = this;
         lbl1.Text = "ToString:";
         lbl1.Location = new Point(dtp.Left, dtp.Bottom + 20);
 
         lblToString = new Label(  );
         lblToString.Parent = this;
         lblToString.Size = new Size(200, 
                                     lblToString.PreferredHeight + 10);
         lblToString.Location = new Point(lbl1.Right, dtp.Bottom + 20);
 
         Label lbl2 = new Label(  );
         lbl2.Parent = this;
         lbl2.Text = "Long Date:";
         lbl2.Location = new Point(dtp.Left, lblToString.Bottom);
 
         lblLongDate = new Label(  );
         lblLongDate.Parent = this;
         lblLongDate.Size = new Size(200, 
                                     lblLongDate.PreferredHeight + 10);
         lblLongDate.Location = new Point(lbl1.Right, 
                                          lblToString.Bottom );
 
         Label lbl3 = new Label(  );
         lbl3.Parent = this;
         lbl3.Text = "Long Time:";
         lbl3.Location = new Point(dtp.Left, lblLongDate.Bottom);
 
         lblLongTime = new Label(  );
         lblLongTime.Parent = this;
         lblLongTime.Size = lblLongDate.Size;
         lblLongTime.Location = new Point(lbl1.Right, lblLongDate.Bottom);
 
         Label lbl4 = new Label(  );
         lbl4.Parent = this;
         lbl4.Text = "Short Date:";
         lbl4.Location = new Point(dtp.Left, lblLongTime.Bottom);
 
         lblShortDate = new Label(  );
         lblShortDate.Parent = this;
         lblShortDate.Size = lblLongDate.Size;
         lblShortDate.Location = new Point(lbl1.Right,
                                           lblLongTime.Bottom);
 
         Label lbl5 = new Label(  );
         lbl5.Parent = this;
         lbl5.Text = "Short Time:";
         lbl5.Location = new Point(dtp.Left, lblShortDate.Bottom);
 
         lblShortTime = new Label(  );
         lblShortTime.Parent = this;
         lblShortTime.Size = lblLongDate.Size;
         lblShortTime.Location = new Point(lbl1.Right,
                                           lblShortDate.Bottom);
 
         Label lbl6 = new Label(  );
         lbl6.Parent = this;
         lbl6.Text = "Custom String:";
         lbl6.Location = new Point(dtp.Left, lblShortTime.Bottom);
 
         txtCustomString = new TextBox(  );
         txtCustomString.Parent = this;
         txtCustomString.Size = lblLongDate.Size;
         txtCustomString.Location = new Point(lbl1.Right,
                                              lblShortTime.Bottom);
         txtCustomString.Text = "D";
         txtCustomString.TextChanged += 
               new EventHandler(txtCustomString_TextChanged);
 
         Label lbl7 = new Label(  );
         lbl7.Parent = this;
         lbl7.Text = "Custom Format:";
         lbl7.Location = new Point(dtp.Left, txtCustomString.Bottom + 5);
 
         lblCustomFormat = new Label(  );
         lblCustomFormat.Parent = this;
         lblCustomFormat.Size = new Size(lblLongDate.Width,
                                         lblLongDate.Height * 3);
         lblCustomFormat.Location = new Point(lbl1.Right,
                                              txtCustomString.Bottom + 5);
      }  //  close for constructor
 
      static void Main(  ) 
      {
         Application.Run(new DTPicker(  ));
      }
 
      private void UpdateLabels(  )
      {
         lblToString.Text = dtp.Value.ToString(  );
         lblLongDate.Text = dtp.Value.ToLongDateString(  );
         lblLongTime.Text = dtp.Value.ToLongTimeString(  );
         lblShortDate.Text = dtp.Value.ToShortDateString(  );
         lblShortTime.Text = dtp.Value.ToShortTimeString(  );
         lblCustomFormat.Text = dtp.Value.ToString(txtCustomString.Text);
      }
 
      private void this_Load(object sender, EventArgs e)
      {
         UpdateLabels(  );
      }
 
      private void dtp_ValueChanged(object sender, EventArgs e)
      {
         UpdateLabels(  );
      }      
 
      private void txtCustomString_TextChanged(object sender, EventArgs e)
      {
         UpdateLabels(  );
      }      
   }      //  close for form class
}         //  close form namespace

 

By Kalit Sikka   Popularity  (23840 Views)